微信小程序map地图

该文章展示了如何在微信小程序中实现地图功能,包括设置地图的初始位置、显示标记、覆盖物以及执行地图操作如获取中心位置、移动到当前位置、平移标记和包含特定点。示例代码包括WXML结构和JS函数,用于交互控制。
摘要由CSDN通过智能技术生成

使用微信小程序实现地图功能

wxml

<map
      id="myMap"
      style="width: 100%; height: 300px;"
      latitude="{{latitude}}"
      longitude="{{longitude}}"
      markers="{{markers}}"
      covers="{{covers}}"
      show-location
    ></map>

js:

Page({
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [{
      id: 1,
      latitude: 23.099994,
      longitude: 113.324520,
      name: 'T.I.T 创意园'
    }],
    covers: [{
      latitude: 23.099994,
      longitude: 113.344520,
      iconPath: '/image/location.png'
    }, {
      latitude: 23.099994,
      longitude: 113.304520,
      iconPath: '/image/location.png'
    }]
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中使用地图路线规划需要使用腾讯地图API。以下是一个简单的示例: 1. 在小程序中引入腾讯地图API: ```javascript const QQMapWX = require('./qqmap-wx-jssdk.min.js'); const qqmapsdk = new QQMapWX({ key: 'yourKey' //在腾讯地图开发平台申请的key }); ``` 2. 获取用户当前位置: ```javascript wx.getLocation({ type: 'gcj02', //定位方式,gcj02为国测局加密坐标系 success: function (res) { const latitude = res.latitude; const longitude = res.longitude; //调用腾讯地图API获取城市信息 qqmapsdk.reverseGeocoder({ location: { latitude: latitude, longitude: longitude }, success: function (res) { console.log(res.result.address_component.city); }, fail: function (res) { console.log(res); } }); }, fail: function (res) { console.log(res); } }); ``` 3. 获取起点和终点位置的经纬度: ```javascript //调用腾讯地图API获取起点和终点位置信息 qqmapsdk.geocoder({ address: '广州市天河区岗顶地铁站', success: function (res) { const fromLatitude = res.result.location.lat; const fromLongitude = res.result.location.lng; qqmapsdk.geocoder({ address: '广州市天河区棠东地铁站', success: function (res) { const toLatitude = res.result.location.lat; const toLongitude = res.result.location.lng; //调用腾讯地图API获取路线规划信息 qqmapsdk.direction({ mode: 'driving', from: { latitude: fromLatitude, longitude: fromLongitude }, to: { latitude: toLatitude, longitude: toLongitude }, success: function (res) { console.log(res); }, fail: function (res) { console.log(res); } }); }, fail: function (res) { console.log(res); } }); }, fail: function (res) { console.log(res); } }); ``` 4. 在地图上显示路线: ```javascript //在地图上显示路线 const polyline = []; const steps = res.result.routes[0].steps; for (let i = 0; i < steps.length; i++) { const polylinePoints = steps[i].polyline; for (let j = 0; j < polylinePoints.length; j++) { const point = polylinePoints[j].split(','); polyline.push({ latitude: parseFloat(point[0]), longitude: parseFloat(point[1]) }); } } that.setData({ polyline: [{ points: polyline, color: "#FF0000DD", width: 4, dottedLine: false }] }); ``` 以上是一个简单的地图路线规划示例,具体实现还需根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值