全局 实例化 AMapWX 对象 引入(amap-wx.js 文件(amap-wx.js 从相关下载页面下载的 zip 文件解压后得到))
var amapFile = require('gaodemap/amap-uni.js');
var amapPlugin = new amapFile.AMapWX({
key: "高德Key"
});
1.获取当前坐标位置信息
amapPlugin.getRegeo({
//成功回调
success: function(data) {
that.zuobiao.latitude = data[0].latitude;
that.zuobiao.longitude = data[0].longitude;
//console.error(data[0].regeocodeData.addressComponent.province + "----------------------------------")
that.zuobiao.province = data[0].regeocodeData.addressComponent.province;
console.log(that.zuobiao.province + "==")
that.zuobiao.city = data[0].regeocodeData.addressComponent.city;
that.zuobiao.district = data[0].regeocodeData.addressComponent.district;
uni.setStorageSync("zuobiao", that.zuobiao);
that.getServerApi(data[0].latitude, data[0].longitude);
},
fail: function(info) {
//失败回调
console.log(info)
// 定位权限未开启,引导设置
uni.showModal({
title: '温馨提示',
content: '您已拒绝定位,请开启',
confirmText: '去设置',
success(res) {
if (res.confirm) {
//打开授权设置
that.openSetting()
}
}
})
}
})
2.传入经纬度,获取坐标地址信息
//坐标转为地址信息
getreverseGeocode(latitude, longitude) {
amapPlugin.getRegeo({
location: '' + longitude + ',' + latitude + '', //location的格式为'经度,纬度'
//成功回调
success: function(data) {
console.error(data)
that.zuobiao.city = data[0].regeocodeData.addressComponent.city; //城市
that.zuobiao.district = data[0].regeocodeData.addressComponent.district;
that.zuobiao.province = data[0].regeocodeData.addressComponent.province;
uni.setStorageSync("zuobiao", that.zuobiao);
},
fail: function(info) {
//失败回调
console.log(info)
// 定位权限未开启,引导设置
uni.showModal({
title: '温馨提示',
content: '您已拒绝定位,请开启',
confirmText: '去设置',
success(res) {
if (res.confirm) {
//打开授权设置
that.openSetting()
}
}
})
}
})
},