uniapp uni.getlocation 返回 "errmsg": "getlocation:fail not support gcj02" 如何解决?

我在uniapp中使用uni.getlocation获取位置信息时,返回了错误提示"errmsg": “getlocation:fail not support gcj02”。请问这是什么原因导致的?应该如何解决这个问题?

2 回复

在manifest.json中配置坐标系类型为wgs84,或检查手机定位权限是否开启。


这个问题是因为 uni.getLocation 方法不支持 gcj02 坐标系导致的。以下是解决方案:

1. 检查坐标系参数 确保将 type 参数设置为 'wgs84'(默认值)或 'gcj02' 以外的可用坐标系:

uni.getLocation({
  type: 'wgs84', // 或检查文档支持的其他坐标系
  success: (res) => {
    console.log(res.latitude, res.longitude);
  },
  fail: (err) => {
    console.error('定位失败:', err);
  }
});

2. 检查运行环境

  • 某些平台(如部分国产安卓设备或特定小程序环境)可能对坐标系有限制
  • 在 H5 端使用需确保网站已部署 HTTPS

3. 使用备用定位方案 如果必须使用 GCJ-02 坐标系:

// 先获取 WGS84 坐标后转换
uni.getLocation({
  type: 'wgs84',
  success: (res) => {
    // 调用坐标转换工具(需自行实现或使用第三方库)
    const gcjCoord = wgs84ToGcj02(res.longitude, res.latitude);
    console.log('转换后坐标:', gcjCoord);
  }
});

4. 检查权限配置

  • 在 manifest.json 中确认已添加定位权限
  • 小程序平台需在 manifest 中配置 requiredPrivateInfos

推荐方案: 直接使用 type: 'wgs84' 即可解决大部分兼容性问题,如需特定坐标系建议通过坐标转换实现。

回到顶部