uni-app中uni.getLocation使用type: "gcj02"报错
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.getLocation
的type
参数支持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);
}
});