前言:分享下app、H5、微信小程序需要使用定位權(quán)限打開地圖的功能,如果拒絕授權(quán),則打開授權(quán)設(shè)置
- 直接上代碼啦
//打開地圖
export function chooseLocation(success){
// 先判斷定位權(quán)限是否開啟
uni.getLocation({
success(){
//定位權(quán)限開啟,打開地圖
uni.chooseLocation({
success // 成功回調(diào)
})
},
fail(e) {
// 定位權(quán)限未開啟,引導(dǎo)設(shè)置
uni.showModal({
title: '溫馨提示',
content: '您已拒絕定位,請開啟',
confirmText: '去設(shè)置',
success(res){
if (res.confirm) {
//打開授權(quán)設(shè)置
openSetting()
}
}
})
}
})
}
//打開授權(quán)設(shè)置(必須用戶點(diǎn)擊小程序才能打開授權(quán)設(shè)置,所以前面加了showModel)
export function openSetting(){
// 打開小程序的設(shè)置
// #ifdef MP-WEIXIN
uni.openSetting()
// #endif
// App跳轉(zhuǎn)系統(tǒng)的設(shè)置界面
// #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
}
- 經(jīng)測試,app、H5、微信小程序三端兼容,上面代碼條件編譯了app、微信小程序這兩種,H5在用戶拒絕授權(quán)后,每次都會自動彈窗詢問,所以不需要處理
好啦,完成!撒花花~~~