在用微信小程序map组件的时候发现的一个问题
在wx.createMapContext("map")创建实例mapCtx之后,调用getMapRegion获取视野范围的时候报错,原因是页面中的地图还未生成。
解决办法增加延迟或者在失败的回调函数中重新执行,注意执行位置防止死循环,或者根据实际情况增加判断
function myGetRegion(mapCtx) {
mapCtx.getRegion({
success: res => {
let dis = this.GetDistance(
res.northeast.latitude,
res.northeast.longitude,
res.southwest.latitude,
res.southwest.longitude
);
return dis
},
fail: e => {
setTimeout(myGetRegion,500)
}
});
}
本文地址:https://blog.csdn.net/weixin_47731144/article/details/108126953