获取当前位置
配置
manifest.json
文件即可
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
"requiredPrivateInfos": ["getLocation"]
完整配置
{
"mp-weixin" : {
/* 小程序特有相关 */
"appid" : "********",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true,
"permission": {
// --------需要设置的位置---------
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
"requiredPrivateInfos": ["getLocation"]
// --------需要设置的位置---------
},
"vueVersion" : "3"
}
const getLocation = () => {
uni.getSetting({
success(settingRes) {
// 判断是否已经授权
if (settingRes.authSetting["scope.userLocation"]) {
// 已授权,直接使用
uni.getLocation({
type: "wgs84",
success(res) {
console.log("获取位置成功", res);
},
});
} else {
// 未授权,尝试申请授权
uni.authorize({
scope: "scope.userLocation",
success() {
// 用户同意授权
uni.getLocation({
type: "wgs84",
success(res) {
console.log("获取位置成功", res);
},
});
},
fail() {
// 用户拒绝授权,弹出设置引导
uni.showModal({
title: "提示",
content: "需要开启位置权限才能使用该功能,是否前往设置?",
success(res) {
if (res.confirm) {
uni.openSetting(); // 打开设置页
}
},
});
},
});
}
},
});
};