微信小程序高德地图获取当前定位

微信小程序高德地图获取当前定位## 微信小程序

//定位方法
getUserLocation: function () {
var _this = this;
wx.getSetting({
success: (res) => {
// res.authSetting[‘scope.userLocation’] == undefined 表示 初始化进入该页面
// res.authSetting[‘scope.userLocation’] == false 表示 非初始化进入该页面,且未授权
// res.authSetting[‘scope.userLocation’] == true 表示 地理位置授权
if (res.authSetting[‘scope.userLocation’] != undefined && res.authSetting[‘scope.userLocation’] != true) {
//未授权
wx.showModal({
title: ‘请求授权当前位置’,
content: ‘需要获取您的地理位置,请确认授权’,
success: function (res) {
if (res.cancel) {
//取消授权
wx.showToast({
title: ‘拒绝授权’,
icon: ‘none’,
duration: 1000
})
} else if (res.confirm) {
//确定授权,通过wx.openSetting发起授权请求
wx.openSetting({
success: function (res) {
if (res.authSetting[“scope.userLocation”] == true) {
wx.showToast({
title: ‘授权成功’,
icon: ‘success’,
duration: 1000
})
//再次授权,调用wx.getLocation的API
_this.gaodeGetLocation();
} else {
wx.showToast({
title: ‘授权失败’,
icon: ‘none’,
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting[‘scope.userLocation’] == undefined) {
//用户首次进入页面,调用wx.getLocation的API
_this.gaodeGetLocation();
}else {
console.log(‘授权成功’)
//调用wx.getLocation的API
_this.gaodeGetLocation();
var page = getCurrentPages().pop();
if (page == undefined || page == null) return;
page.onLoad();
}
}
})
},
//通过两点经纬度计算距离(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(“计算结果”,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);
//计算两个经纬度距离
var jl = that.distance(39.928712, 116.393345, res[0].latitude, res[0].longitude);
console.log(“打印计算两个点的距离:”+jl);
this.setData({
gaodeAddress:res[0].regeocodeData.addressComponent.city,
})
}
})
},

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值