// #ifdef MP-WEIXIN
//定位方法
getUserLocation: function() {
var _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
})
_this.locationRole = false;
} else if (res.confirm) {
//確定授權,通過wx.openSetting發(fā)起授權請求
wx.openSetting({
success: function(res) {
if (res.authSetting[
"scope.userLocation"] ==
true) {
wx.showToast({
title: '授權成功',
icon: 'success',
duration: 1000
})
_this.locationRole = true;
//再次授權,調用wx.getLocation的API
_this.geo();
} else {
wx.showToast({
title: '授權失敗',
icon: 'none',
duration: 1000
})
_this.locationRole = false;
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//用戶首次進入頁面,調用wx.getLocation的API
_this.geo();
} else {
console.log('授權成功')
_this.locationRole = true;
//調用wx.getLocation的API
_this.geo();
}
}
})
},
// 獲取定位城市
geo: function() {
// var _this = this;
// wx.getLocation({
// type: 'wgs84',
// success: function(res) {
// _this.latitude = res.latitude
// _this.longitude = res.longitude
// _this.stationDetails()
// }
// })
var _this = this;
// wx.showLoading({
// title: '定位中...',
// mask: true,
// })
wx.getLocation({
// type: 'wgs84',
type: 'gcj02',
success: function(res) {
console.log('location111', res)
_this.latitude = res.latitude
_this.longitude = res.longitude
wx.hideLoading()
// 調用得方法==============
// _this.stationDetails()
}
});
// new Promise((resolve, reject) => {
// let _locationChangeFn = (res) => {
// console.log('location change', res)
// _this.latitude = res.latitude
// _this.longitude = res.longitude
// _this.stationDetails()
// wx.hideLoading()
// // wx.offLocationChange(_locationChangeFn)
// }
// wx.startLocationUpdate({
// success: (res) => {
// console.log(res, 5656);
// wx.onLocationChange(_locationChangeFn)
// resolve()
// },
// fail: (err) => {
// console.log('獲取當前位置失敗', err)
// wx.hideLoading()
// reject()
// }
// })
// })
},
// #endif