-- 创造无限可能

uniapp微信小程序获取位置信息

2024-01-22 17:17:12
1482 人浏览 0 人点赞
有用,点赞支持一下

微信小程序用户授权获取用户位置信息
1、配置文件manifest.json,选择“源码视图”,添加配置

2、上代码

            getLocationInfo(){
                uni.getLocation({
                  type: 'gcj02',
                  success: function(res) {
                    const latitude = res.latitude; // 纬度
                    const longitude = res.longitude; // 经度

                    // 调用逆地理编码API将经纬度转换为地址
                    uni.request({
                      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
                      data: {
                        location: `${latitude},${longitude}`,
                        key: '你的key' //腾讯地图开放平台上申请并获得了Web服务API密钥
                      },
                      success: function(response) {
                        const address = response.data.result.address;

                        // 在此处处理获取到的地址信息
                        console.log("在此处处理获取到的地址信息" + address);

                        this.form.get_car_address = address
                      }
                    })
                  }
                })
            }

3、如果没有key,去腾讯地图开放平台上申请并获得了Web服务API密钥
地址:https://lbs.qq.com/dev/console/home

4、常见问题
4.1 此key未开启WebserviceAPI功能

4.2 getLocation:fail the api need to be declared in the requiredPrivateInfos fie
这个问题查看第一步,没有配置manifest.json导致的