在开发者工具中显示正常,在android手机上调用异常:
在android手机上,地图控件隐藏后在显示,wx.createMapContext(mapId).getCenterLocation方法没有任何回调
代码如下:
<view class="map-container" wx:if="{{stopPlaceIsShowMap}}">
<map id="mymap" bindregionchange="stopPlaceRegionchange" longitude="{{stopPlaceLongitude}}" latitude="{{stopPlaceLatitude}}" scale="16" show-location="true" controls="{{stopPlaceControls}}" style="width: 100%; height:100%;"></map>
</view>
Page({
onReady: function (e) { // 使用 wx.createMapContext 获取 map 上下文
this.mapCtx = wx.createMapContext('myMap')
},
getCenterLocation: function () {
this.mapCtx.getCenterLocation({
success: function(res){
console.log(res)
},
fail: function(res){
console.log(res)
}
})
}
})
如果stopPlaceIsShowMap的值为false之后,在置为true,调用getCenterLocation方法,没有任何回调,在开发工具中正常,不知道算不算是个bug
hidden 是所有组件公共的属性,你试一下
stopPlaceIsShowMap 为 false 时,map 组件还没有添加到节点树上,onReady 时创建 context 就有可能找不到控件,可以尝试一下这两种方法:
1、用 map 的 hidden 替换成 wx:if
2、stopPlaceIsShowMap 变成 true 之后再去创建 context + getCenterLocation
如何动态的添加hidden属性?
试了重新创建context,也一样没有反应;map控件没有hidden属性啊,如果添加hidden,css属性自动新增display:none;
通过display:none或block也能实现该效果
是啊,以为代码逻辑有问题,调试了很久在定位到具体问题
新坑?
目前是通过别的方式绕过去的