<view class="form-option" bindtap='getSetting'>
<input placeholder="請選擇收貨地址" type="text" name="" bindinput="" value='{{address}}' />
</view>
//首次進入,點擊‘選擇地址’
![圖片描述][1]
//彈出詢問框
![圖片描述][2]
拒絕授權後,再次進入該頁面或者點擊頁面某按鈕(獲取位置)綁定JS,再打開授權。
![圖片描述][3]![圖片描述][4]
getSetting:function(){ //獲取用戶的當前設置
const _this = this;
wx.getSetting({
success: (res) => {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化進入該頁面
// res.authSetting['scope.userLocation'] == false 表示 非初始化進入該頁面,且未授權
// res.authSetting['scope.userLocation'] == true 表示 地理位置授權
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
//未授權
wx.showModal({
title: '請求授權當前位置',
content: '需要獲取您的地理位置,請確認授權',
success: function (res) {
if (res.cancel) {
//取消授權
wx.showToast({
title: '拒絕授權',
icon: 'none',
duration: 1000
})
} else if (res.confirm) {
//確定授權,通過wx.openSetting發起授權請求
wx.openSetting({
success: function (res) {
if (res.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授權成功',
icon: 'success',
duration: 1000
})
//再次授權,調用wx.getLocation的API
_this.goAddress();
} else {
wx.showToast({
title: '授權失敗',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//用戶首次進入頁面,調用wx.getLocation的API
_this.goAddress();
}
else {
// console.log('授權成功')
//調用wx.getLocation的API
_this.goAddress();
}
}
})
},
第二步:在需要獲取地址的頁面中:
wx.getLocation({
type: "wgs84",
success: function (res) {
_this.setData({
longitude: options.lat ? Number(options.lng) : res.longitude,
latitude: options.lng ? Number(options.lat) : res.latitude,
markers: [{
latitude: options.lat ? Number(options.lat) : res.latitude,
longitude: options.lng ? Number(options.lng) : res.longitude,
}]
})
if (options.lat){
_this.setData({
address: options.address,
})
} else {
qqmapsdk.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success: function (addressRes) {
const result = addressRes.result;
_this.setData({
address: result.address,
})
}
})
}
},
fail:function(){
wx.navigateBack({
delta:1
})
}
})