<button bindtap="tomap" class="cu-btn line-blue sm">获取地址</button>
tomap: function() {
var t = this;
wx.authorize({
scope: "scope.userLocation",
success: function(i) {
wx.chooseLocation({
success: function(i) {
t.setData({
latitude: i.latitude,
longitude: i.longitude,
address: i.address
}), e = new a({
key: "HEQBZ-R6TWR-3YHWF-WJACM-ZH6LE-3SFB6"
});
var d = i.latitude, s = i.longitude;
t.getCity(d, s);
},
fail: function() {
wx.getSetting({
success: function(e) {
e.authSetting["scope.userLocation"] || wx.showModal({
content: "请允许获取地理位置后再次尝试",
success: function(e) {
e.confirm ? wx.openSetting({
success: function(e) {
e.authSetting = {
"scope.userInfo": !0,
"scope.userLocation": !0
};
},
fail: function(e) {
console.log(e);
}
}) : e.cancel && console.log("用户点击取消");
}
});
}
});
}
});
}
});
},
获取定位2
methods: {
isGetLocation() { // 3. 检查当前是否已经授权访问scope属性,参考下截图
var _this = this;
let a = "scope.userLocation";
uni.getSetting({
success(res) {
if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
_this.getAuthorizeInfo()
} else {
_this.getLocation()
}
}
});
},
getAuthorizeInfo() { //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
var _this = this;
let a = "scope.userLocation";
uni.authorize({
scope: a,
success() { //1.1 允许授权
_this.getLocation();
},
fail() { //1.2 拒绝授权
console.log("你拒绝了授权,无法获得周边信息 ")
// 弹出授权确认框
_this.getSetFun();
}
})
},
// 没有获取到位置,不停获取
getSetFun() {
let _this = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '是否授权当前位置',
content: '请确认授权,否则无法正常使用',
success(res) {
if (res.confirm) {
wx.openSetting({
success() {
// 设置获取允许获取位置后重新调用获取定位方法或者跳到首页
_this.getLocation();
}
})
} else if (res.cancel) {
// 跳到首页
}
}
})
} else {
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
wx.showModal({
title: '您手机定位功能没有开启',
content: '请在系统设置中打开定位服务',
success() {
// 跳到首页
}
})
}
}
})
},
getLocation() {
var that = this;
var myAmapFun = new amapFile.AMapWX({
key: mapKey
});
myAmapFun.getPoiAround({
iconPath: 'https://s2.ax1x.com/2020/03/10/8CvKmt.png',
iconWidth: 22,
iconHeight: 32,
success: function(reset) {
that.initMap(reset.markers[0])
// console.error("高德地图", JSON.stringify(reset))
},
fail: function(info) {
uni.showToast({
icon: 'none’',
title: '位置获取失败,请允许小程序获取位置权限',
duration: 2000
})
}
})
},
// 初始化我的位置
async initMap(res) {
// this.longitude = res.longitude;
// this.latitude = res.latitude;
this.addrss = await this.getAddressName(res);
console.error("定位", this.addrss);
// this.addressObj = Object.assign({}, this.addressObj,{
// longitude: res.longitude,
// latitude: res.latitude,
// address: this.myAddress
// })
// console.error('初始化我的位置:' + JSON.stringify(this.addressObj));
},
// 查询地图中心点的名称
getAddressName(addressObj) {
let that = this;
let url = 'https://restapi.amap.com/v3/geocode/regeo?key=' + "7e38dcb5deed0f1f27360f8f4f63c3bf" +
"&location=" + addressObj.longitude + "," + addressObj.latitude + "&extensions=all";
console.log("url", url);
return new Promise((resrt) => {
uni.request({
url: url,
data: {
// key: "c1560fe15fca882115da0018d7cf42df",
// location: addressObj.longitude + "," + addressObj.latitude,
// extensions: "all"
// s: "rsx",
// sdkversion: "sdkversion",
// logversion: "logversion"
},
success: function(e) {
// console.log("响应数据", JSON.stringify(e));
let addressComponent = e.data.regeocode.addressComponent;
let addrss = addressComponent.city +
addressComponent.district +
addressComponent.streetNumber.street +
addressComponent.streetNumber.number;
// resrt(e.data.regeocode.formatted_address); // 省市区街道几号
resrt(addrss);
// that.setData({
// textData: {
// name: res.data.regeocode.pois[0].name,
// desc: res.data.regeocode.formatted_address
// }
// })
// console.log(JSON.stringify(res.data.regeocode.formatted_address));
},
fail: function(rest) {
uni.showToast({
icon: 'none’',
title: '位置获取失败',
duration: 2000
})
resrt(rest);
}
});
})
},
}