//获取定位
export function location() {
return new Promise((resolve, reject) => {
if (vuex.state.userLocation) {
resolve(vuex.state.userLocation)
}
uni.getLocation({
type: 'gcj02',
success: function(res) {
let userLocation = {
longitude: res.longitude, //当前位置的经度
latitude: res.latitude //当前位置的纬度
}
vuex.commit('userLocation', userLocation)
resolve(userLocation)
},
fail: function(res) {
checkUserLocation(() => {
uni.getLocation({
type: 'gcj02',
success: function(res) {
let userLocation = {
longitude: res.longitude, //当前位置的经度
latitude: res.latitude //当前位置的纬度
}
vuex.commit('userLocation', userLocation)
resolve(userLocation)
}
});
})
}
});
})
}
//给用户重新授权弹窗
const checkUserLocation = function() {
uni.getSetting({
success(res) {
if (!res.authSetting.hasOwnProperty('scope.userLocation')) {
uni.authorize({
scope: 'scope.userLocation',
success() {
if (typeof callback == 'function') {
callback();
}
},
fail: function() {
checkUserLocation();
}
})
} else if (res.authSetting.hasOwnProperty('scope.userLocation') && !res.authSetting[
'scope.userLocation']) {
uni.showModal({
title: '请授权地理定位否则无法为您服务',
content: '',
cancelText: '暂不开启',
confirmText: '开启授权',
confirmColor: '#FF9E57',
success: function(tip) {
if (tip.confirm) {
uni.openSetting({
success: function(data) {
if (typeof callback == 'function') {
callback();
}
},
fail: function() {
checkUserLocation();
}
})
} else {
callback();
// checkUserLocation();
}
},
})
} else {
if (typeof callback == 'function') {
callback();
}
}
},
fail: function() {
checkUserLocation();
}
})
}
微信小程序 封装获取定位
最新推荐文章于 2025-03-27 13:38:53 发布