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: "您拒绝了授权当前位置",
});
}
},
});
},
});
});
};