1.manifest.json配置
/* 小程序特有相关 */
"mp-weixin": {
"appid": "wx735**********9b3f",
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
"requiredPrivateInfos": ["getLocation"]
},
2.页面使用 直接cv 在fnGetlocation()方法内操作返回的经纬度数据
// 定位授权
getLocation() {
let that = this;
// 判断手机定位服务【GPS】 是否授权
uni.getSystemInfo({
success(res) {
let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
if (locationEnabled == false || locationAuthorized == false) {
//手机定位服务(GPS)未授权
that.$common.toast('请打开手机GPS');
} else {
//手机定位服务(GPS)已授权
//判断微信小程序是否授权位置信息
uni.authorize({
scope: 'scope.userLocation',
success: res => {
that.fnGetlocation();
},
fail: err => {
err = err['errMsg'];
uni.showModal({
content: '需要授权位置信息',
confirmText: '确认授权'
}).then(res => {
console.log(res);
if (res[1]['confirm']) {
uni.openSetting({
success: res => {
if (res.authSetting[
'scope.userLocation'
]) {
that.$common.toast('授权成功');
that.fnGetlocation();
} else {
// 未授权
that.$common.toast(
'授权失败,请重新授权');
uni.showModal({
title: '授权',
content: '获取授权' +
authouName +
'失败,是否前往授权设置?',
success: function(
result
) {
if (result
.confirm
) {
uni
.openSetting();
}
},
fail: function() {
that.$common
.toast(
'系统错误!'
);
}
});
}
}
});
}
if (res[1]['cancel']) {
// 取消授权
that.$common.toast('你拒绝了授权,无法获得周边信息');
}
});
}
});
}
}
});
},
// 定位获取
fnGetlocation() {
let that = this;
uni.getLocation({
type: 'wgs84', //返回 gps 坐标
geocode: 'true',
isHighAccuracy: 'true',
accuracy: 'best', // 精度值为20m
success: function(res) {
// 在这里操作经纬度数据!!!!!!!!!!
that.bindList.long = res.longitude;
that.bindList.lat = res.latitude;
uni.setStorageSync('longlat', res.longitude + ',' + res.latitude);
console.log('自动定位' + res.errMsg);
},
fail(err) {
if (err.errMsg === 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化') {
// that.$common.toast('请勿频繁定位')
}
if (err.errMsg === 'getLocation:fail auth deny') {
// 未授权
that.$common.toast('无法定位,请重新获取位置信息');
authDenyCb && authDenyCb();
that.isLocated = false;
}
if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
uni.showModal({
content: '请开启手机定位服务',
showCancel: false
});
}
}
});
}