微信小程序多点标记,导航

哪位指点一下,想弄个多点标记。并且点击标记点能直接导航,现在做的只能导航同一个标记点!

小程序的微信开发文档有说明的

以下内容部分参考ChatGPT模型:


你可以使用微信小程序的地图组件来实现多点标记和导航功能。具体步骤如下:

  1. 在wxml文件中添加地图组件,并设置地图中心点和缩放级别:
<map id="myMap" latitude="{{latitude}}" longitude="{{longitude}}" scale="{{scale}}"></map>
  1. 在js文件中获取当前位置,并将地图中心点设置为当前位置:
wx.getLocation({
  type: 'gcj02',
  success: (res) => {
    this.setData({
      latitude: res.latitude,
      longitude: res.longitude
    })
  }
})
  1. 在js文件中定义多个标记点,并将其添加到地图上:
wx.getLocation({
  type: 'gcj02',
  success: (res) => {
    this.setData({
      latitude: res.latitude,
      longitude: res.longitude
    })

    // 添加标记点
    const markers = [{
      id: 1,
      latitude: res.latitude,
      longitude: res.longitude,
      title: '我的位置'
    }, {
      id: 2,
      latitude: res.latitude + 0.01,
      longitude: res.longitude + 0.01,
      title: '标记点1'
    }, {
      id: 3,
      latitude: res.latitude - 0.01,
      longitude: res.longitude - 0.01,
      title: '标记点2'
    }]
    this.setData({
      markers: markers
    })
  }
})
  1. 在wxml文件中监听标记点的点击事件,并获取标记点的经纬度:
<map id="myMap" latitude="{{latitude}}" longitude="{{longitude}}" scale="{{scale}}" bindmarkertap="onMarkerTap" markers="{{markers}}"></map>
  1. 在js文件中实现标记点的点击事件,调用微信小程序的导航API实现导航功能:
onMarkerTap(e) {
  const markerId = e.markerId
  const marker = this.data.markers.filter((marker) => marker.id === markerId)[0]

  wx.openLocation({
    latitude: marker.latitude,
    longitude: marker.longitude,
    name: marker.title
  })
}

以上就是实现微信小程序多点标记和导航功能的思路和示例代码。


如果我的建议对您有帮助、请点击采纳、祝您生活愉快