微信小程序如何将地图中心移动到当前定位点?
微信小程序中,可以使用 'moveToLocation()' 方法将地图中心移动到当前定位点。
代码示例:
wx.getLocation({
type: 'gcj02', // 默认为 wgs84 返回 gps 坐标,gcj02 返回国测局坐标
success (res) {
const latitude = res.latitude;
const longitude = res.longitude;
this.mapCtx.moveToLocation({
latitude: latitude,
longitude: longitude,
duration: 1000, // 动画时长,单位为毫秒
});
}
});
注意:
- 需要在小程序配置文件 app.json 中声明使用地图组件:
'usingComponents': {
'mpvue-map': '/components/mpvue-map/mpvue-map'
}
- 确保已开启用户位置授权。
原文地址: https://www.cveoy.top/t/topic/nOsQ 著作权归作者所有。请勿转载和采集!