const moveToAndScale = async (lat, lng, scale = 16) => {
if (!mapCtx.value){
setTimeout(() => moveToAndScale(lat, lng), 200)
return
}
setTimeout(()=>{
mapCtx.value.moveToLocation({
latitude: lat,
longitude: lng,
success: () => {
// 加一个延时,避免与动画冲突
setTimeout(() => {
latitude.value = lat
longitude.value = lng
mapScale.value = scale
}, 400)
},
fail: (err) => {
console.error("moveToLocation 失败", err)
}
})
},1000)
}
在h5页面是没有问题的,到微信小程序实机调试就遇到这个问题了
解决了.
我说一下我的问题,微信小程序实机调试报:moveToMapLocation:fail:mapview is null ,但是H5页面是没有问题的.就小程序一直不行. 看了别人写的代码我才发现问题 mapContext = uni.createMapContext("map", instance) 我的天原来H5可以不传instance,但是微信一定要传 instance
还有一个就是如果你在H5,这样写大概还是不行,但在微信小程序没问题
onReady(()=>{
controls.value = [ { id: 1, position: { left: 20, top: 20, width: 40, height: 40, }, iconPath: mapIcon, clickable: true }]
nextTick(() => {
mapContext = uni.createMapContext("map", instance)
})
})
h5 中@updated 回调中初始化 MapContext const onMapUpdated = () => {
if (markers.value && markers.value.length) {
setTimeout(() => { markers.value = [...markers.value] }, 120)
}
mapContext = uni.createMapContext("map", instance)
}
const updateIncludePoints = () => {
includePoints.value = polygon.value.map(m => ({
latitude: m.latitude,
longitude: m.longitude
}))
}
0 个回复