主要参考以下两篇文章,插眼以保存
(8条消息) 根据两点经纬度坐标计算距离_OSKernelLAB(gatieme)-CSDN博客_根据两点经纬度坐标计算距离
(8条消息) 根据经纬度计算两地距离_weixin_34218890的博客-CSDN博客
computeDistance() {
var that = this
// 计算纬度差
var lat1 = that.latitude * Math.PI / 180.0
var lat2 = that.childLatitude * Math.PI / 180.0
var latdistinction = Math.abs(lat1 - lat2)
// 计算经度差
var long1 = that.longitude * Math.PI / 180.0
var long2 = that.childLongitude * Math.PI / 180.0
var longdistinction = Math.abs(long1 - long2)
//计算两点之间的实际距离(经过我的查询这个公式叫做:Haversine公式)
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(latdistinction / 2), 2) + Math.cos(lat1) * Math.cos(
lat2) * Math.pow(Math.sin(longdistinction / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
//保留最后两位小数
s = s.toString();
s = s.substring(0, s.indexOf('.') + 2);
console.log(s)
that.distance = s
//提醒一下,这里的获取的距离,也就是s的单位是Km
// 判断方向
if (that.childLatitude > that.latitude) {
that.north = true
} else {
that.north = false
}
if (that.childLongitude > that.childLongitude) {
that.east = true
} else {
that.east = false
}
},