- 申请getLocation接口权限
- 使用getLocation接口前,还要在app.json文件中配置
"permission": { "scope.userLocation": { "desc": "同意此应用使用你的地理位置" } }
- 在需要定位的页面编写,获取到当前的经纬度(定位有些许偏差)
wx.getLocation({ isHighAccuracy: true, // 开启地图精准定位 type: 'gcj02', success:res=>{ this.setData({ latitude:res.latitude, longitude:res.longitude }) }, fail:err=>{ console.error('获取位置失败',err) } })
4.结合Map组件一起使用,在地图上显示当前所在的位置
<map latitude="{{latitude}}" longitude="{{longitude}}" scale="18" show-location="true"></map>
效果图: