1.引入文件
import QQMapWX from "../../utils/qqmap-wx-jssdk.js"
下载地址: 微信小程序JavaScript SDK | 腾讯位置服务
2.获取位置信息
// 获取位置信息
getLocationInfo() {
return new Promise((resolve) => {
//位置信息默认数据
let location = {
longitude: 0,
latitude: 0,
province: "",
city: "",
area: "",
street: "",
address: "",
};
uni.getLocation({
type: "gcj02",
success(res) {
location.longitude = res.longitude;
location.latitude = res.latitude;
// 腾讯地图Api
const qqmapsdk = new QQMapWX({
key: '' //申请的key 用自己的
});
qqmapsdk.reverseGeocoder({
location,
success(response) {
console.log('response', response)
let info = response.result;
location.province = info.address_component.province;
location.city = info.address_component.city;
location.area = info.address_component.district;
location.street = info.address_component.street;
location.address = info.address;
resolve(location);
},
});
},
fail(err) {
resolve(location);
},
});
});
},
3. then返回省市区值
this.getLocationInfo().then(res => {
this.city_name = res.province //省
this.city_city = res.city //市
this.city_area = res.area //区
this.address = this.city_area ? this.city_area : this.city_city ? this.city_city : this.city_name
})
注:腾讯地图的key申请地址