1.申请高德账号,创建应用获取key
2.具体代码
newpositioning(){
var _this=this;
uni.getLocation({
type: 'wgs84',
success: function (res) {
_this.lon=res.longitude;
_this.lat=res.latitude;
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
uni.request({
header:{"Content-Type": "application/text"},
//注意:这里的key值需要高德地图的 web服务生成的key 只有web服务才有逆地理编码
url:'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location='+res.longitude+','+res.latitude+'&key=申请的key&radius=1000&extensions=all',
success(re) {
console.log(re)
if(re.statusCode===200){
// that.citydata=re.data.regeocode.addressComponent.city
if(!re.data.regeocode.addressComponent.city==[]){
_this.city=re.data.regeocode.addressComponent.city;
}
_this.city=re.data.regeocode.addressComponent.province;
console.log("获取中文街道地理位置成功",_this.city)
uni.setStorageSync('city',_this.city);
}else{
console.log("获取信息失败,请重试!")
}
}
});
}
});
},