小程序计算两点间的距离并显示出来

时间:2024-03-18 21:07:49

onLoad: function (options) {
//[0: (39.928712, 116.393345),1: (39.928722, 116.393853)] 两个坐标
this.distance(39.928712, 116.393345, 39.928722, 116.393853)
},
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; 计算出来的值

///////////////////////////////////////////////////////////////////////

获取本地坐标里的js中写入 res本地坐标
var distance = that.distance(res.latitude, res.longitude,39.918034,116.415192);
console.log(“当前位置距离北京故宫:”, distance, “千米”)

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;
s = s.toFixed(2);
return s;
},

小程序计算两点间的距离并显示出来
小程序计算两点间的距离并显示出来