//高德地图js
import amapFile from '@/libs/amap-wx.130.js';
/**
* 获取位置信息
*/
const checkauthorize = () => {
// #ifdef H5
getLocation();
//#endif
//#ifdef MP-WEIXIN
uni.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
uni.authorize({
scope: 'scope.userLocation',
success: () => { //这里是用户同意授权后的回调
getLocation();
},
fail: () => { //这里是用户拒绝授权后的回调
rejectSetting()
}
})
} else { //用户已经授权过了
getLocation();
}
}
})
//#endif
}
const getLocation = () => {
uni.getLocation({
type: 'wgs84',
success: function (res) {
console.log(res)
console.log('经度:' + res.longitude);
mapList.value.latitude = res.latitude
mapList.value.longitude = res.longitude
mapList.value.covers = []
mapList.value.covers.push({
latitude: res.latitude,
longitude: res.longitude,
iconPath: '/static/image/driver.png',
width: 30,
height: 30
})
console.log('纬度:' + res.latitude);
getAddress(res.latitude, res.longitude);
}
});
}
//经纬度转换成省市区 latitude纬度,long经度
const getAddress = (latitude, longitude) => {
let myAmapFun = new amapFile.AMapWX({ key: "高德地图key" });
myAmapFun.getRegeo({
location: '' + longitude + ',' + latitude + '',//location的格式为'经度,纬度'
success: function (data) {
console.log("转换成省市", data);
let { province, city, district } = data[0].regeocodeData.addressComponent;
city = (city || city?.length > 0) ? city : "";
console.log(city)
// nowCity.value = city;
// uni.setStorageSync('city', nowCity.value)
console.log("省市区:", province + city + district)
},
fail: function (info) { }
})
}
//用户拒绝授权后的回调
const rejectSetting = () => {
let that = this;
uni.showModal({
title: '警告',
content: '授权失败,请打开位置消息授权',
success: (res) => {
if (res.confirm) { //去授权
toOpenSetting();
} else if (res.cancel) {//用户点击取消
console.log(res);
uni.showModal({
title: '提示',
content: '获取权限失败,将无法获取定位哦~',
showCancel: false,
success: (res) => {
toOpenSetting();
}
})
}
}
})
}
//打开微信设置页面
const toOpenSetting = () => {
uni.openSetting({
success: (e) => {
console.log(e);
}
})
}
- THE END -
最后修改:2025年1月6日
非特殊说明,本博所有文章均为博主原创。