uniapp小程序获取用户授权自己的位置权限

uniapp获取用户授权自己的位置权限

如果是小程序中大部分地方需要用到定位,可以写在vue的原型上面,如果只是个别需要,可以直接当成方法写在里面,下面是写在原型上的示例,

首先,需要在mainifest.json中配置位置的权限,如果用的是vscode的话,,如下配置即可

在这里插入图片描述

 "permission" : {
   "scope.userLocation" : {
        "desc" : "你的位置信息将用于为您就近匹配门店"
    }
}

如果是hbuilderX,也是mainifest.json文件

在这里插入图片描述

最后在main.js入口文件中写入

// 全局获取位置
Vue.prototype.getLocation = function () {
  return new Promise(function (resolve, reject) {
    showLoading();
    uni.getLocation({
      type: "wgs84",
      success: function (res) {
        hideLoading();
        resolve(res);
      },
      fail: function () {
        hideLoading();
        uni.showModal({
          title: "请求授权当前位置",
          content: "我们需要获取地理位置信息,为您推荐附近的店面",
          success: (res) => {
            if (res.confirm) {
              uni.openSetting().then((res) => {
                if (res[1].authSetting["scope.userLocation"] === true) {
                  uni.getLocation({
                    type: "wgs84",
                    success: function (res) {
                      resolve(res);
                    },
                  });
                } else {
                  uni.showToast({
                    title: "您拒绝了授权当前位置",
                  });
                }
              });
            } else if (res.cancel) {
              uni.showToast({
                title: "您拒绝了授权当前位置",
              });
            }
          },
        });
      },
    });
  });
};
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值