前言:分享下app、H5、微信小程序需要使用定位权限打开地图的功能,如果拒绝授权,则打开授权设置
- 直接上代码啦
//打开地图
export function chooseLocation(success){
// 先判断定位权限是否开启
uni.getLocation({
success(){
//定位权限开启,打开地图
uni.chooseLocation({
success // 成功回调
})
},
fail(e) {
// 定位权限未开启,引导设置
uni.showModal({
title: '温馨提示',
content: '您已拒绝定位,请开启',
confirmText: '去设置',
success(res){
if (res.confirm) {
//打开授权设置
openSetting()
}
}
})
}
})
}
//打开授权设置(必须用户点击小程序才能打开授权设置,所以前面加了showModel)
export function openSetting(){
// 打开小程序的设置
// #ifdef MP-WEIXIN
uni.openSetting()
// #endif
// App跳转系统的设置界面
// #ifdef APP-PLUS
uni.getSystemInfo({
success(res) {
if(res.platform=='ios'){ //IOS
plus.runtime.openURL("app-settings://");
} else if (res.platform=='android'){ //安卓
let main = plus.android.runtimeMainActivity();
let Intent = plus.android.importClass("android.content.Intent");
let mIntent = new Intent('android.settings.ACTION_SETTINGS');
main.startActivity(mIntent);
}
}
});
// #endif
}
- 经测试,app、H5、微信小程序三端兼容,上面代码条件编译了app、微信小程序这两种,H5在用户拒绝授权后,每次都会自动弹窗询问,所以不需要处理
好啦,完成!撒花花~~~