uni-app中uni.getLocation使用type: "gcj02"报错

发布于 1周前 作者 yibo5220 来自 Uni-App

uni-app中uni.getLocation使用type: "gcj02"报错

操作步骤:

uni.getLocation({
type: "gcj02",
geocode: true,
success: function (res) {
console.log(res)
},
fail: (err) => {
uni.showToast({
title:'定位失败',
icon:"none",
})
console.log(JSON.stringify(err))
}
});

预期结果:

可正常返回定位信息


### 实际结果:

报错 jsLog: {"errMsg":"getLocation:fail getLocation:fail Not Support CoordsType,https://ask.dcloud.net.cn/article/282"} at components/zl-map/zl-map.vue:164

bug描述:

uni.getLocation({  
    type: "gcj02",  
    geocode: true,  
    success: function (res) {  
        console.log(res)  
    },  
    fail: (err) => {  
        uni.showToast({  
            title:'定位失败',  
            icon:"none",  
        })  
        console.log(JSON.stringify(err))  
    }  
});

在 uni 代码中使用以上方法 ios 中返回错误

jsLog: {“errMsg”:“getLocation:fail getLocation:fail Not Support CoordsType,https://ask.dcloud.net.cn/article/282”} at components/zl-map/zl-map.vue:164


请问如何处理  
type: "gcj02",不传的话可以获取到位置 但在搞的地图中渲染是偏离的

1 回复

uni-app 中使用 uni.getLocation 获取地理位置时,如果你指定了 type: "gcj02" 并且遇到报错,可能是因为以下原因:

1. API 支持问题

  • uni.getLocationtype 参数支持 wgs84(默认)和 gcj02 两种坐标系。
  • 如果你的 uni-app 版本较旧,可能不支持 gcj02 类型。建议更新 uni-app 到最新版本。

2. 平台差异

  • uni.getLocation 在不同平台(如微信小程序、H5、App)上的实现可能有所不同。某些平台可能不支持 gcj02 类型。
  • 微信小程序:微信小程序原生支持 gcj02 类型,但需要在 app.json 中配置 permission
  • H5:H5 环境下可能不支持 gcj02,默认使用 wgs84
  • App:App 环境下通常支持 gcj02,但需要确保使用的 SDK 版本是最新的。

3. 权限问题

  • 在某些平台上(如微信小程序、App),获取地理位置需要用户授权。如果用户未授权,可能会导致报错。
  • 确保你在调用 uni.getLocation 之前已经获得了用户的地理位置权限。

4. 代码示例

以下是一个使用 uni.getLocation 并指定 type: "gcj02" 的示例代码:

uni.getLocation({
    type: 'gcj02',
    success: function (res) {
        console.log('当前位置的经度:' + res.longitude);
        console.log('当前位置的纬度:' + res.latitude);
    },
    fail: function (err) {
        console.error('获取位置失败:', err);
    }
});

5. 错误处理

  • 如果仍然遇到报错,建议在 fail 回调中打印错误信息,以便更好地定位问题。

6. 检查平台支持

  • 你可以通过 uni.getSystemInfo 检查当前运行平台,并根据平台特性调整代码。
uni.getSystemInfo({
    success: function (res) {
        console.log('当前运行平台:', res.platform);
    }
});
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!