getLocation() {
uni.getLocation({
type: 'gcj02',// 使用国测局坐标系
isHighAccuracy: true, // 开启高精度定位
success: (res) => {
console.log(res)
this.latitude = res.latitude; // 纬度
this.longitude = res.longitude; // 经度
console.log('纬度:', this.latitude);
console.log('经度:', this.longitude);
},
fail: (err) => {
console.error('获取位置失败', err);
}
});
}
如果需要获取详细的地址信息,可以结合高德地图或腾讯地图的逆地理编码API
// 逆地理转地址
turnAddress() {
// 高德逆地理变码 https://lbs.amap.com/api/webservice/guide/api/georegeo/
let that = this;
uni.request({
method: 'GET',
url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
data: {
key: 'YOUR_AMAP_APPKEY',// 替换为你的高德地图AppKey
location: `${that.longitude},${that.latitude}`,
output: 'JSON'
},
success: async (res) => {
if (res.statusCode === 200 && res.data.status === '1') {
//用户所在的地理位置信息
that.address = res.data.regeocode.formatted_address
} else {
console.error('逆地理编码失败:', res.data);
}
},
fail: err => {
console.log(err);
}
});
}
需要使用第三方地图库,得在manifest.json配置文件上按需配置,如我的h5则需要在web配置上
配置高德的key