微信小程序--怎样在小程序中创建地图并渲染数据中的点标记

效果:

首先--创建地图

使用官方文档中的地图组件 map

<map id="mapId" class="map" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}"></map>

其中的属性值:

longitude  经度

latitude  纬度

markers  点标记

wxml部分:

<view>
  <view class="top">
    <view class="topLeft">
      <van-tabs bind:click="onClick">
        <van-tab wx:for="{{addressData}}" wx:key="id" name="{{item.id}}" title="{{item.name}}">
        </van-tab>
      </van-tabs>
    </view>
    <view class="topRight">
      <image src="img/mapbtn.png" mode="" />
    </view>
  </view>
  <map id="mapId" class="map" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}"></map>
</view>

js部分:

/**
   * 页面的初始数据
   */
  data: {
    addressData: [],
    addressId: '',
    sencinData: [],
    longitude: '116.397963',
    latitude: '39.915119',
    markers: []
  },
// 点击事件
  onClick(event) {
    console.log(event);
    this.addressId = event.detail.name
    wx.showToast({
      title: `点击标签 ${event.detail.title}`,
      icon: 'none',
    });
    let that = this
    wx.request({
      url: 'https://xxxxxx.xxx', //接口请求
      method: 'POST',
      data: {
        xxx: event.detail.name
      },
      success: (res) => {
        console.log(res);
        that.setData({
          sencinData: res.data.data
        })
        console.log(that.data.sencinData);
        this.loadMarkers(); //点标记渲染
      }
    })
  },
//渲染事件
  loadMarkers: function () {
    let markers = this.data.sencinData
    //生成 markers 列表,用于在地图上展示
    let markersData = markers.map(marker => {
      return {
        id: marker.id,
        longitude: marker.lng, //经度
        latitude: marker.lat, //纬度
        title: marker.name, //名称
        county: marker.address, 
        iconPath: 'https://xxxxxx' + marker.imgs, //标点图片,接口数据如果没有可以用本地路径
        width: 40,
        height: 40,
      };
    });
    console.log('---------------------------');
    console.log(markersData);
    console.log('---------------------------');
    this.setData({
      markers: markersData
    });
  },

生命周期函数--监听页面初次渲染完成:

/**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    wx.setNavigationBarTitle({
      title: '景区地图',
    })
    // 初始渲染
    let that = this
    wx.request({
      url: 'https://xxxxxx.xxx',
      method: 'POST',
      data: {
        xxx: xxx
      },
      success: (res) => {
        console.log(res);
        that.setData({
          addressData: res.data.data
        })
        console.log(that.data.addressData);
        wx.request({
          url: 'https://xxxxxx.xxx',
          method: 'POST',
          data: {
            xxx: that.data.xxx[0].id
          },
          success: (res) => {
            console.log(res);
            that.setData({
              sencinData: res.data.data
            })
            console.log(that.data.sencinData);
            //加载点位数据
            this.loadMarkers();
          }
        })
      }
    })

  },

最终效果:

我这里的数据经纬度都是一样的(因为是练习的demo),所以呈现了上述效果

/ヾ(≧▽≦*)o

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码
根据提供的引用内容,实现微信小程序 scroll-view 的 scroll-into-view 无效果可能是由以下几个原因导致的: 1. scroll-into-view 属性的值与要跳转到的锚标记的 id 属性值一致。例如,如果要跳转到一个 id 为 "anchor1" 的锚标记,那么 scroll-into-view 的值应该设置为 "anchor1"。 2. scroll-view 容器高度不足:如果 scroll-view 容器的高度不足以显示要跳转到的锚标记,那么 scroll-into-view 将无法生效。确保 scroll-view 容器的高度足够大,以容纳所有的内容和要跳转到的锚标记。 3. scroll-into-view 设置在子组件上:scroll-into-view 属性应该设置在 scroll-view 组件上,而不是其子组件上。确保 scroll-into-view 属性被正确地设置在 scroll-view 组件上。 4. scroll-into-view 设置在动态生成的内容上:如果要跳转到的锚标记是在动态生成的内容,那么需要在动态生成内容之后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容渲染完成后才能生效。 5. scroll-into-view 设置在隐藏的内容上:如果要跳转到的锚标记是在初始状态下是隐藏的内容,那么需要在显示该内容后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容显示后才能生效。 以下是一个示例代码,演示了如何在微信小程序使用 scroll-view 的 scroll-into-view 实现锚跳转效果: ```html <scroll-view scroll-into-view="{{toView}}" scroll-y="true" style="height: 300px;"> <view id="anchor1">锚1</view> <view id="anchor2">锚2</view> <view id="anchor3">锚3</view> <view id="anchor4">锚4</view> <view id="anchor5">锚5</view> </scroll-view> <button bindtap="scrollToAnchor">跳转到锚2</button> ``` ```javascript Page({ data: { toView: '' }, scrollToAnchor: function() { this.setData({ toView: 'anchor2' }) } }) ``` 在上述示例,scroll-view 组件设置了 scroll-into-view 属性为 toView 变量的值,当击按钮时,toView 变量的值被设置为 "anchor2",从而实现了跳转到锚2的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值