1.首先获取经纬度
getLocation(){
let that = this;
uni.getLocation({
type: 'wgs84',
success: function (res) {
that.getCity(Number(res.latitude),Number(res.longitude));
}
});
},
2.安装vue-jsonp, main.js全局引入
npm install vue-jsonp --save
import {VueJsonp} from 'vue-jsonp';
Vue.use(VueJsonp);
3.根据经纬度获取地址
getCity(latitude,longitude){
let that = this;
that.$jsonp("https://apis.map.qq.com/ws/geocoder/v1/", {
key: "", // 腾讯地图申请的key uniapp 推荐使用腾讯地图
callbackName: "getJsonData",
output: 'jsonp',
location: latitude+","+longitude
})
.then(json => {
// 请求成功的返回数据
console.log(json.result.ad_info.city);
})
.catch(err => {
// 请求失败的返回数据
console.log(err)
})
},