H5移动端页面获取到地理定位后怎么计算与另一个地点之间的距离?

H5移动端页面获取到地理定位后怎么计算与另一个地点之间的距离

呼如林
浏览 1164回答 1
1回答

杨__羊羊

function distance(latA, lngA, latB, lngB) {&nbsp; //longitude and latitude; 经&nbsp; &nbsp;纬&nbsp; &nbsp;度&nbsp; &nbsp; var earthR = 6371000;&nbsp; &nbsp; var x = Math.cos(latA*Math.PI/180) * Math.cos(latB*Math.PI/180) * Math.cos((lngA-lngB)*Math.PI/180);&nbsp; &nbsp; var y = Math.sin(latA*Math.PI/180) * Math.sin(latB*Math.PI/180);&nbsp; &nbsp; var s = x + y;&nbsp; &nbsp; if (s > 1) {&nbsp; &nbsp; &nbsp; &nbsp; s = 1;&nbsp; &nbsp; }&nbsp; &nbsp; if (s < -1) {&nbsp; &nbsp; &nbsp; &nbsp; s = -1;&nbsp; &nbsp; }&nbsp; &nbsp; var alpha = Math.acos(s);&nbsp; &nbsp; var distance = alpha * earthR;&nbsp; &nbsp; return distance;}如果都是经纬度的话,上面的方式就可以
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript