先说下思路:
1.申请腾讯地图的key https://lbs.qq.com/
2.申请好key之后,下载SDK,放在项目里,下载地址
3.申请好key,文件也下载好了之后,最后一步,在小程序后台request里添加 https://apis.map.qq.com 为合法域名
上述都是准备工作,下面开始上代码
//引入刚才下载的文件
var QQMapWX = require("../../libs/qqmap-wx-jssdk.js");
//js部分
getDingwei() {
var that = this;
const tMap = new QQMapWX({
key: "你申请的key", //开发者密钥
});
uni.getLocation({
type: "gcj02",
geocode: true,
success: (res) => {
console.log("👍👍获取经纬度成功", res);
that.longitude = res.longitude;
that.latitude = res.latitude;
tMap.reverseGeocoder({
location: {
latitude: that.latitude,
longitude: that.longitude,
},
success: function (res) {
console.log("👍👍当前地址:", res.result.address);
},
fail: function (res) {
console.log("👍👍定位失败", res);
},
});
},
fail: () => {
console.log("获取经纬度失败");
},
});
},
最后看一下打印(开发者工具有时候打印不出来,也没有获取地址的授权框,建议真机调试)