2637关注7518浏览
用户拖动地图后,需要得到前后地图两中心点的距离
- 希望提供的能力
地图组件map能否有一个api可以得到当前地图视野范围中心点,并且提供一个api可以计算两中心点坐标距离
以下是个人代码片段
// 视野发生变化时触发
bindregionchange(e){
console.log(e);
let that = this;
this.mapCtx.getCenterLocation({
success:function(res){
let startLoc={
latitude:'',
longitude:''
};
let endLoc = {
latitude: '',
longitude: ''
};
if(e.type==='begin'){
startLoc.latitude = res.latitude;
startLoc.latitude = res.longitude
}else{
startLoc.latitude = res.latitude;
startLoc.latitude = res.longitude;
//如果map中心点坐标没有变就不需要重新请求数据
let isCenterChange = that.data.latitude === that.data.latitudeCenter &&
that.data.longitude === that.data.longitudeCenter;
if (!isCenterChange) {
that.handleinitMap({ latitude: res.latitude, longitude: res.longitude });
}
}
//测两点距离
// let distance = that.distance(startLoc.latitude,startLoc.longitude,endLoc.longitude,endLoc.longitude);
// if (distance > 13000){
// console.log(distance);
// }
},
});
},
// 计算两点的距离
distance(la1, lo1, la2, lo2) {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = Math.abs(La1 - La2);
var Lb3 = Math.abs(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;
return s;
},
-
至过去的我
2044人对此回答表示赞同
我是未来的你,你现在是不是在年找寻小程序答案。你不要感觉诧异,给你来信原因,就是让你不在后悔。今天去学习如何推广小程序,相信......点击查看更多> -
Strawberry8
8人对此回答表示赞同
你的反馈我们已收到,我们会在后续的版本中考虑增加类似功能。
展开80回复分享发布于 6年前评论(0)
收起评论