getPositioning() {
let that = this;
uni.getLocation({
type: "gcj02", //默认为 wgs84 返回 gps 坐标
geocode: "true",
isHighAccuracy: true,
// accuracy: "best", // 精度值为20m
success: function (res) {
console.log("定位获取:", res);
console.log("经纬度: ", res.latitude, res.longitude)
// 逆地理解析
uni.request({
url: 'https://restapi.amap.com/v3/geocode/regeo',
method: 'GET',
data: {
location: res.longitude + ',' + res.latitude,
key: '高德key', // web服务,其他的不行
},
success: function (res) {
that.address = res.data.regeocode.formatted_address
console.log(res.data.regeocode.formatted_address);
},
fail: function (err) {
console.log('地址解析失败' + err);
}
})
},
fail(err) {
if (
err.errMsg ===
"getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化"
) {
uni.showToast({
title: "请勿频繁定位",
icon: "none",
});
}
if (err.errMsg === "getLocation:fail auth deny") {
// 未授权
uni.showToast({
title: "无法定位,请重新获取位置信息",
icon: "none",
});
authDenyCb && authDenyCb();
that.isLocated = false;
}
if (
err.errMsg ===
"getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
) {
uni.showModal({
content: "请开启手机定位服务",
showCancel: false,
});
}
console.log(err.errMsg, '111')
},
});
},
uniapp.getLocation 获取当前经纬度以及逆地理解析
最新推荐文章于 2024-05-21 17:04:51 发布