目录

uniapp-vue3-微信小程序-uni.chooseLocation使用

uniapp vue3 微信小程序 uni.chooseLocation使用

申请

先要去 申请使用接口

https://i-blog.csdnimg.cn/direct/747c70f457084d1a9e4ab74a2e378918.png

开通成功之后就可以在项目中配置使用了

配置

配置 manifest.json

https://i-blog.csdnimg.cn/direct/d282fb3f396f4f648d0e212cb86d73ee.png

"mp-weixin": {
    /* 小程序特有相关 */
    "requiredPrivateInfos": ["chooseLocation"],
    "permission": {
      "scope.userLocation": {
        "desc": "你的小程序需要获取你的位置信息,以便为你提供更精准的服务。"
      }
    }
}

使用

uni.chooseLocation({
    success: (res) => {
      // 更新位置信息
      console.log('位置名称:', res.name);
      console.log('详细地址:', res.address);
    },
    fail: (err) => {
      console.error('选择位置失败:', err.errMsg);
      // 这里可以根据不同的错误码进行不同的处理
      if (err.errMsg.includes('auth deny')) {
        uni.showModal({
          title: '提示',
          content: '你拒绝了位置授权,若需要使用该功能,请在小程序设置中开启位置权限。',
          success: (modalRes) => {
            if (modalRes.confirm) {
              // 引导用户去设置页面开启权限
              uni.openSetting();
            }
          }
        });
      }
    }
  });