uni-app getCenterLocation无返回

uni-app getCenterLocation无返回

开发环境 版本号 项目创建方式
Windows win10 21H1 HBuilderX
### 操作步骤:
- getCenterLocation无返回

### 预期结果:
- getCenterLocation该有返回值

### 实际结果:
- 无

### bug描述:

<map class="mapTb" id="mapTb" ref="mapTb" :latitude="latitude" :longitude="longitude" scale="15" :markers="marker"@regionchange="getCenter"></map>
const mapContext = uni.createMapContext('mapTb', this);
getCenter: function() {
const vm = this;
console.log(mapContext)
// mapContext.getCenterLocation(function(res){
//      console.log(res)
// });
mapContext.getCenterLocation({
success: (res) => {
console.log('res=====' + res)
vm.marker.push({
latitude: res.latitude,
longitude: res.longitude,
width: 32, //宽
height: 32, //
alpha: 0.5, //透明度
iconPath: '../../static/images/place.png',
})
},
fail: (data) => {
console.log('fail=====' + data)
},
complete: (data) => {
console.log('complete========' + data)
}
})
}
  • getCenter函数已调用,mapContext也能打印出来,但是getCenterLocation的success,fail,complete均无返回

更多关于uni-app getCenterLocation无返回的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app getCenterLocation无返回的实战教程也可以访问 https://www.itying.com/category-93-b0.html


根据你的代码分析,getCenterLocation无返回可能有以下几个原因:

  1. 调用时机问题getCenterLocation需要在regionchange事件触发时调用,但地图初始化完成后才会触发。建议在onReady生命周期中初始化地图上下文,并设置一个标志位确保地图已准备就绪。

  2. 作用域问题mapContext在组件外定义可能导致上下文丢失。建议在getCenter函数内部或onReady中创建:

    getCenter: function() {
      const mapContext = uni.createMapContext('mapTb', this);
      // 其余代码
    }
回到顶部