以下代码第一次进入到页面时,不能够自动定位中心经纬度为当前位置,退出当前界面再重新进去时,才会定位中心点位置为当前位置
<map id="myMap" style="width: 100%; height: 100%;" latitude="{{currlat}}" longitude="{{currlon}}" show-location="{{true}}" />
Page({
data: {
currlat: 0, // 初始纬度
currlon: 0, // 初始经度
},
onLoad: function () {
let that = this
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的经纬度
success: function (res) {
that.setData({
currlon:res.longitude,
currlat:res.latitude
})
},
fail: function (res) {
util.showToast(res)
}
})
},
});
试试在
wx.getLocation
的success
回调中调用map
组件的moveToLocation
方法,强制地图移动到最新坐标。success(res) { this.setData({ currlat: res.latitude, currlon: res.longitude }, () => { const mapCtx = wx.createMapContext('myMap'); mapCtx.moveToLocation(); // 手动居中视野 }); }
wx.getLocation的isHighAccuracy参数设置为true试下
打印看看第一次赋值了吗