微信小程序高德地图获取当前定位
微信小程序高德地圖獲取當(dāng)前定位## 微信小程序
//定位方法
getUserLocation: function () {
var _this = this;
wx.getSetting({
success: (res) => {
// res.authSetting[‘scope.userLocation’] == undefined 表示 初始化進(jìn)入該頁(yè)面
// res.authSetting[‘scope.userLocation’] == false 表示 非初始化進(jìn)入該頁(yè)面,且未授權(quán)
// res.authSetting[‘scope.userLocation’] == true 表示 地理位置授權(quán)
if (res.authSetting[‘scope.userLocation’] != undefined && res.authSetting[‘scope.userLocation’] != true) {
//未授權(quán)
wx.showModal({
title: ‘請(qǐng)求授權(quán)當(dāng)前位置’,
content: ‘需要獲取您的地理位置,請(qǐng)確認(rèn)授權(quán)’,
success: function (res) {
if (res.cancel) {
//取消授權(quán)
wx.showToast({
title: ‘拒絕授權(quán)’,
icon: ‘none’,
duration: 1000
})
} else if (res.confirm) {
//確定授權(quán),通過(guò)wx.openSetting發(fā)起授權(quán)請(qǐng)求
wx.openSetting({
success: function (res) {
if (res.authSetting[“scope.userLocation”] == true) {
wx.showToast({
title: ‘授權(quán)成功’,
icon: ‘success’,
duration: 1000
})
//再次授權(quán),調(diào)用wx.getLocation的API
_this.gaodeGetLocation();
} else {
wx.showToast({
title: ‘授權(quán)失敗’,
icon: ‘none’,
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting[‘scope.userLocation’] == undefined) {
//用戶首次進(jìn)入頁(yè)面,調(diào)用wx.getLocation的API
_this.gaodeGetLocation();
}else {
console.log(‘授權(quán)成功’)
//調(diào)用wx.getLocation的API
_this.gaodeGetLocation();
var page = getCurrentPages().pop();
if (page == undefined || page == null) return;
page.onLoad();
}
}
})
},
//通過(guò)兩點(diǎn)經(jīng)緯度計(jì)算距離(KM)
distance:function(la1, lo1, la2, lo2) {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
s = s * 6378.137;//地球半徑
s = Math.round(s * 10000) / 10000;
console.log(“計(jì)算結(jié)果”,s);
return s;
},
//高德地圖獲取定位
gaodeGetLocation:function(){
var that = this;
var myAmapFun=new aMap.AMapWX({key:’*********自己的高德地圖key’});
myAmapFun.getRegeo({
success:(res)=>{
console.log(res)
wx.setStorageSync(‘gaodeAddress’,res[0].regeocodeData.addressComponent.city)
//console.log(“高德地圖獲取定位:”+JSON.stringify(res),res[0].regeocodeData.formatted_address);
//計(jì)算兩個(gè)經(jīng)緯度距離
var jl = that.distance(39.928712, 116.393345, res[0].latitude, res[0].longitude);
console.log(“打印計(jì)算兩個(gè)點(diǎn)的距離:”+jl);
this.setData({
gaodeAddress:res[0].regeocodeData.addressComponent.city,
})
}
})
},
總結(jié)
以上是生活随笔為你收集整理的微信小程序高德地图获取当前定位的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle create table
- 下一篇: 使用VMware搭建一个局域网网络拓扑