uni-app uni.getLocation() API type: 'gcj02' 定位报错 getLocation:fail not support gcj02

uni-app uni.getLocation() API type: ‘gcj02’ 定位报错 getLocation:fail not support gcj02

开发环境 版本号 项目创建方式
Windows 21H2 HBuilderX
Android Android 11
小米
红米
产品分类:uniapp/App

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:21H2(OS 内部版本 22000.2538)

HBuilderX类型:正式

HBuilderX版本号:4.15

手机系统:Android

手机系统版本号:Android 11

手机厂商:小米

手机机型:红米

页面类型:vue

vue版本:vue3

打包方式:云端

项目创建方式:HBuilderX

### 示例代码:

```javascript
//定位
gps() {
    // console.log('获取当前位置')
    uni.showLoading({
        title: '获取位置中',
        mask: true
    })
    let __this = this;
    uni.getLocation({
        type: 'gcj02',
        isHighAccuracy: true, // 是否高精度获取
        highAccuracyExpireTime: 3000,
        timeout: '3',
        cacheTimeout: 3,
        accuracy: 'best',
        success: function(res) {
            console.log(res, '会计法艰苦撒旦发')
        },  
        fail(erro) {  
            console.log(erro, '111会计法艰苦撒旦发')  
        },  
    });
}

操作步骤:

直接调用就行

预期结果:

获取经纬度

实际结果:

" errMsg ": "getLocation:fail not support gcj02", " errCode ": 18, " code ": 18

bug描述:

uni.getLocation() api type: ‘gcj02’, 定位报 getLocation:fail not support gcj02,我们配置了高德地图的key


更多关于uni-app uni.getLocation() API type: 'gcj02' 定位报错 getLocation:fail not support gcj02的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

看了这个的帖子https://ask.dcloud.net.cn/question/173880,加上这个geolocation-amap-release.aar包就好了

更多关于uni-app uni.getLocation() API type: 'gcj02' 定位报错 getLocation:fail not support gcj02的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在使用 uni-appuni.getLocation() API 时,如果你指定了 type: 'gcj02' 并且遇到了 getLocation:fail not support gcj02 的错误,这通常意味着当前平台或设备不支持 gcj02 坐标系。

解决方法:

  1. 检查平台支持

    • gcj02 是中国特有的坐标系,主要用于中国大陆地区。如果你在非中国的设备或平台上使用,可能不会支持 gcj02 坐标系。
    • 确保你的应用在中国大陆的设备或平台上运行。
  2. 使用默认坐标系

    • 如果不确定是否支持 gcj02,可以尝试不指定 type,或者使用 wgs84(默认的全球坐标系)。
    uni.getLocation({
        type: 'wgs84',
        success: function (res) {
            console.log('当前位置的经度:' + res.longitude);
            console.log('当前位置的纬度:' + res.latitude);
        },
        fail: function (err) {
            console.error('获取位置失败:', err);
        }
    });
    
  3. 使用第三方地图服务

    • 如果你确实需要使用 gcj02 坐标系,但平台不支持,可以考虑使用第三方地图服务(如高德地图、百度地图等)来获取 gcj02 坐标。
    • 这些地图服务通常提供 SDK 或 API 来获取 gcj02 坐标。
  4. 检查权限

    • 确保应用已经获取了定位权限。如果没有权限,uni.getLocation() 也会失败。
  5. 更新 uni-app 版本

    • 如果你使用的是较旧版本的 uni-app,尝试更新到最新版本,看看是否解决了问题。
  6. 处理不支持的情况

    • 在代码中添加对不支持 gcj02 坐标系的处理逻辑,例如提示用户或自动切换到 wgs84

示例代码:

uni.getLocation({
    type: 'gcj02',
    success: function (res) {
        console.log('当前位置的经度:' + res.longitude);
        console.log('当前位置的纬度:' + res.latitude);
    },
    fail: function (err) {
        console.error('获取位置失败:', err);
        if (err.errMsg === 'getLocation:fail not support gcj02') {
            // 处理不支持 gcj02 的情况
            uni.showToast({
                title: '当前设备不支持GCJ02坐标系',
                icon: 'none'
            });
            // 尝试使用 wgs84
            uni.getLocation({
                type: 'wgs84',
                success: function (res) {
                    console.log('当前位置的经度:' + res.longitude);
                    console.log('当前位置的纬度:' + res.latitude);
                },
                fail: function (err) {
                    console.error('获取位置失败:', err);
                }
            });
        }
    }
});
回到顶部