用户2901302
用户2901302
  • 发布:2025-10-01 18:01
  • 更新:2025-10-09 17:53
  • 阅读:103

uniapp小程序,腾讯地图开发,调用moveToLocation遇到errMsg: "moveToMapLocation:fail:mapview is null"

分类:uni-app

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
}))
}

2025-10-01 18:01 负责人:无 分享
已邀请:
DCloud_UNI_OttoJi

DCloud_UNI_OttoJi - 日常回复 uni-app/x 问题,如果艾特我没看到,请主动私信

感谢反馈,第二个参数应该是可选的,后续文档会进一步编辑描述。

问题一,请提供个复现工程。如果你是在页面中使用 map 组件,是不需要第二参数的,你是在子组件中调用了吗,提供个复现工程吧

  • DCloud_UNI_OttoJi

    问题一,请提供个复现工程。如果你是在页面中使用 map 组件,是不需要第二参数的,你是在子组件中调用了吗,提供个复现工程吧

    2025-10-09 17:53

  • DCloud_UNI_OttoJi

    回复 DCloud_UNI_OttoJi: 问题二,h5 是有具体报错吗,也提供个复现工程吧。

    2025-10-09 17:54

要回复问题请先登录注册