getCenterLocation方法在uni-app app端返回空{}无法获取到经纬度

getCenterLocation方法在uni-app app端返回空{}无法获取到经纬度

项目信息 详情
产品分类 uniapp/App
PC开发环境 Windows
PC操作系统版本 11
HBuilderX类型 正式
HBuilderX版本 4.66
手机系统 Android
手机系统版本 Android 16
手机厂商 小米
手机机型 小米
页面类型 vue
Vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

示例代码:

this.mapContext.getCenterLocation({
// type: 'gcj02',
// geocode: true,
// isHighAccuracy: true,
// altitude: true,
success: (res) => {
console.log(res)
this.location.longitude = res.longitude
this.location.latitude = res.latitude
if (this.enableLocationScope) {
const discount = this.jnpf.getDistance(this.currentLocation.latitude, this
.currentLocation.longitude, this.location.latitude, this.location.longitude
) || 0;
if (discount > (this.adjustmentScope || 500)) return this.$refs.uTips.show({
title: '超出微调范围',
type: 'warning',
});
}
this.getList()
}
})

操作步骤:

预期结果:

获取到中心点坐标

实际结果:

返回{},获取不到中心点坐标

bug描述:

在app真机上调用地图的getCenterLocation方法,获取不到中心店坐标,res返回是{}


更多关于getCenterLocation方法在uni-app app端返回空{}无法获取到经纬度的实战教程也可以访问 https://www.itying.com/category-93-b0.html

13 回复

你加个fail逻辑,看下有报错吗?

更多关于getCenterLocation方法在uni-app app端返回空{}无法获取到经纬度的实战教程也可以访问 https://www.itying.com/category-93-b0.html


测试了,没有进入fail,截图在下面

回复 9***@qq.com: 也可能是hbuilderx的问题,是控制台不显示了,但是实际上是有值的,你直接打印res中的数值试一下,如果它没.latitude之类的,那么应该直接报错

回复 DCloud_UNI_yuhe测了一下,直接打印是undefined

回复 9***@qq.com: 你之前是好的吗?是升级产生的吗?使用的自定义基座吗?

回复 DCloud_UNI_yuhe:之前是可以的,是直接运行到安卓app基座

测试了,没有进入fail

搞不懂什么问题


有结果吗

你好,遇见同样的问题,升级之后,ios手机上通过getCenterLocation获取中心点位置,返回{}, 没有返回经纬度,请问解决了吗?麻烦看到回复一下

你好这个问题有解决么

我用4.29的sdk可以获取到经纬度,升级后获取的就是空的

在uni-app App端使用getCenterLocation方法返回空对象的问题,通常有以下几个原因和解决方案:

  1. 地图组件未正确初始化 确保map组件已正确渲染并加载完成,建议在onReady回调中调用getCenterLocation方法:
<map id="myMap" @ready="onMapReady"></map>

onMapReady() {
  this.mapContext = uni.createMapContext('myMap', this)
  this.getCenterLocation()
}
  1. 坐标系类型问题 虽然你注释了type参数,但建议明确指定坐标系类型:
this.mapContext.getCenterLocation({
  type: 'gcj02', // 明确指定坐标系
  success: (res) => {
    // ...
  }
})
  1. 权限问题 确保已正确配置定位权限:
  • 在manifest.json中配置Android权限:
"permission": {
  "android.permission.ACCESS_FINE_LOCATION": {}
}
  1. 异步加载问题 地图组件需要时间加载,建议添加延迟:
setTimeout(() => {
  this.mapContext.getCenterLocation({
    // ...
  })
}, 500)
回到顶部