我把核心代码贴出来大家一看就知道
但是要注意的是这个必须在腾讯地图的开放平台中申请自己key然后添加到自己的服务器中,这个我在上一边uniapp实现定位中详细写过,这里我就先不多做赘述了,大家可以去看看。这个地图我就先以腾讯地图为例,高德地图都可以。uniapp实现获取用户位置信息(经纬度),通过高德地图和腾讯地图解析_uniapp获取当前位置经纬度_子伟-H5的博客-CSDN博客
<view @tap="navigateToMapMer">
<img src="../../../static/images/icon/dizhi.png" alt="" style="width: 20px; height: 20px" />
</view>
navigateToMapMer() {
const address = this.merInfo.address;
if (address) {
uni.request({
url: "https://apis.map.qq.com/ws/geocoder/v1/",
data: {
address: address,
key: "HMUBZ-PQLEG-6I3QM-Q67MV-7FXK3-FTF6H",
},
success: function (res) {
if (res.data.status === 347) {
uni.showToast({
icon: "error",
title: "地址查询无结果",
});
}
const location = res.data.result.location;
const latitude = location.lat;
const longitude = location.lng;
console.log(location, "纬度:" + latitude + ",经度:" + longitude);
uni.openLocation({
name: address,
address: address,
latitude: latitude,
longitude: longitude,
success: () => {
console.log("打开地图成功");
},
fail: (err) => {
console.error("打开地图失败", err);
},
});
},
fail: function (err) {
console.error("获取地址经纬度失败", err);
},
});
}
},
上述代码是在uniapp中的源码,可以直接使用,另外你自己换成自己需要的地址就行了,另外如果有疑问可以直接@我联系我如果对你有用麻烦您点个关注谢谢,非常感谢!