在小程序中,不管是地圖還是獲取位置信息烛占,都首先需要獲得用戶的位置許可授權(quán)胎挎。
而為了確保在調(diào)用一些其他方法的時候已經(jīng)提前獲取到用戶位置授權(quán),就必須在操作其他步驟之前忆家,先強制獲得用戶位置授權(quán)犹菇。不然往下的業(yè)務(wù)邏輯也沒法繼續(xù)。
封裝方法芽卿,調(diào)用此方法時揭芍,能夠引導用戶進行授權(quán),如果用戶沒有授權(quán)蹬竖,將會跳轉(zhuǎn)到手動授權(quán)頁面
getUserCity(fn,type){
wx.getLocation({
type: type?type:'wgs84',
success :(res)=> {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
fn(res)
},
fail(ress){
// 檢查授權(quán)
wx.getSetting({
success: (res) => {
// console.log(res.authSetting);
//已授權(quán)就是true
if (res.authSetting['scope.userLocation']) {
this.getUserPosition((res, flag) => {
// console.log(res, flag);
if (flag.flag) {
fn(res)
}
});
} else {
//如果未授權(quán) 打開彈窗
wx.showModal({
title: '請授權(quán)位置權(quán)限',
showCancel: false,
cancelColor: 'cancelColor',
success: () => {
wx.openSetting({
complete: (res) => {},
})
}
})
}
}
})
}
})
},
方法調(diào)用
兩個參數(shù)沼沈,第一個參數(shù)是一個函數(shù)回調(diào)
第二個參數(shù)是傳入獲取的經(jīng)緯度類型 分為wgs84和gcj02 一種是給百度地圖 高德地圖等使用 一種是給小程序內(nèi)部地圖使用
app.getUserCity((res)=>{
console.log(res)
res會返回已經(jīng)獲取到的經(jīng)緯度
})
},'gcj02')
效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者