// 判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
getUserLocation: function () {
let that = 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({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//再次授权,调用wx.getLocation的API
that.getLocation();
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//调用wx.getLocation的API
that.getLocation();
}
else {
//调用wx.getLocation的API
that.getLocation();
}
}
})
},
// 获取定位当前位置的经纬度
getLocation: function () {
wx.getLocation({
type: 'wgs84',
success: function (res) {
let latitude = res.latitude//纬纬
let longitude = res.longitude//经纬
let speed = res.speed//速度
let accuracy = res.accuracy;//位置精确度
wx.setStorageSync('latitude', latitude);
wx.setStorageSync('longitude', longitude);
console.log(`当前位置:维度${latitude} ,经度:${longitude},速度:${speed},精确度:${accuracy}`);
},
fail: function (res) {
console.log('GPS获取失败');
wx.setStorageSync('latitude', '');
wx.setStorageSync('longitude', '');
}
})
},
小程序获取GPS
最新推荐文章于 2024-05-28 16:31:01 发布