微信小程序 现在 的 地图定位是需要申请的。
import amapFile from "../../common/amap-wx.130"; // 引入微信地图的SDK
getLocation(lat = '', lon = '', addr = '') {
console.log('getLocationgetLocation')
// if (addr == '未知地址') { // 微信获取地理位置的方法。
uni.authorize({
scope: 'scope.userLocation',
success() {
console.log('用户未开启权限')
var myAmapFun = new amapFile.AMapWX({
key: '9e3b273b3a6b2792065fba31a74a534e'
});
uni.getLocation({
type: 'gcj02',
success: function(res) {
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
lat = res.latitude;
lon = res.longitude;
console.log(lat, lon, addr)
// check token
myAmapFun.getRegeo({
location: `${lon},${lat}`,
success(data) {
let str = {
province: '',
city: ''
}
// 拿到省份
str.province = data[0].regeocodeData
.addressComponent.province;
// 拿到城市
str.city = data[0].regeocodeData.addressComponent
.city;
if (Object.prototype.toString.call(str.city) ==
"[object Array]") {
str.city = str.city.join('');
}
addr = data[0].regeocodeData.formatted_address;
console.log();
wx.openLocation({
latitude: lat, // 纬度,范围为-90~90,负数表示南纬
longitude: lon, // 经度,范围为-180~180,负数表示西经
scale: 15, // 缩放比例
address: addr ? addr : '',
success: function(r) {
console.log(r)
}
})
},
fail(err) {
console.log('333333333333333', err)
}
})
}
});
},
fail(e) {
uni.showToast({
title: '用户未开启权限',
icon: 'none'
})
setTimeout(() => {
uni.openSetting({
success(res) {
console.log(res.authSetting)
}
})
}, 1000)
console.log(e);
// 判断用户是否拒绝了授权
// wx.getSetting({
// success: res => {
// if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
// // 用户拒绝了授权
// wx.showModal({
// title: '提示',
// content: '您拒绝了定位权限,将无法使用XX功能',
// success: res => {
// if (res.confirm) {
// // 跳转设置页面
// wx.openSetting({
// success: res => {
// if (res.authSetting['scope.userLocation']) {
// // 授权成功,重新定位
// wx.getLocation({
// success: res => {}
// });
// } else {
// // 没有允许定位权限
// wx.showToast({
// title: '您拒绝了定位权限,将无法使用XX功能',
// icon: 'none'
// });
// }
// }
// });
// }
// }
// });
// }}
// })
}
})
// } else {
// console.log(lat, lon, addr)
// wx.openLocation({
// latitude: lat, // 纬度,范围为-90~90,负数表示南纬
// longitude: lon, // 经度,范围为-180~180,负数表示西经
// scale: 15, // 缩放比例
// address: addr ? addr : '',
// success: function(r) {
// console.log(r)
// }
// })
// }
},
微信小程序 获取地理位置使用
最新推荐文章于 2024-04-06 21:41:31 发布