计算两个GPS坐标之间的距离

计算两个GPS坐标之间的距离

如何计算两个GPS坐标之间的距离(使用纬度和经度)?



富国沪深
浏览 1959回答 3
3回答

繁星coding

用经纬度计算两个坐标之间的距离,包括Javascript实现。西和南地点是阴性的。记住,分秒是60秒,所以S31 30‘是-31.50度。别忘了将度转换为弧度..许多语言都有这个功能。或者这是一个简单的计算:radians = degrees * PI / 180.function degreesToRadians(degrees) {   return degrees * Math.PI / 180; } function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {   var earthRadiusKm = 6371;   var dLat = degreesToRadians(lat2-lat1);   var dLon = degreesToRadians(lon2-lon1);   lat1 = degreesToRadians(lat1);   lat2 = degreesToRadians(lat2);   var a = Math.sin(dLat/2) * Math.sin(dLat/2) +           Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));    return earthRadiusKm * c; }下面是一些用法示例:相同点之间的距离(0,0,0,0)/应为0 0距离(51.5,0,38.8,-77.1)/从伦敦到阿灵顿5918.185064088764

动漫人物

使用Google寻找haversine;以下是我的解决方案:#include&nbsp;<math.h> #include&nbsp;"haversine.h" #define&nbsp;d2r&nbsp;(M_PI&nbsp;/&nbsp;180.0) //calculate&nbsp;haversine&nbsp;distance&nbsp;for&nbsp;linear&nbsp;distance double&nbsp;haversine_km(double&nbsp;lat1,&nbsp;double&nbsp;long1,&nbsp;double&nbsp;lat2,&nbsp;double&nbsp;long2) { &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;dlong&nbsp;=&nbsp;(long2&nbsp;-&nbsp;long1)&nbsp;*&nbsp;d2r; &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;dlat&nbsp;=&nbsp;(lat2&nbsp;-&nbsp;lat1)&nbsp;*&nbsp;d2r; &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;a&nbsp;=&nbsp;pow(sin(dlat/2.0),&nbsp;2)&nbsp;+&nbsp;cos(lat1*d2r)&nbsp;*&nbsp;cos(lat2*d2r)&nbsp;*&nbsp;pow(sin(dlong/2.0),&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;c&nbsp;=&nbsp;2&nbsp;*&nbsp;atan2(sqrt(a),&nbsp;sqrt(1-a)); &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;d&nbsp;=&nbsp;6367&nbsp;*&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;d; } double&nbsp;haversine_mi(double&nbsp;lat1,&nbsp;double&nbsp;long1,&nbsp;double&nbsp;lat2,&nbsp;double&nbsp;long2) { &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;dlong&nbsp;=&nbsp;(long2&nbsp;-&nbsp;long1)&nbsp;*&nbsp;d2r; &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;dlat&nbsp;=&nbsp;(lat2&nbsp;-&nbsp;lat1)&nbsp;*&nbsp;d2r; &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;a&nbsp;=&nbsp;pow(sin(dlat/2.0),&nbsp;2)&nbsp;+&nbsp;cos(lat1*d2r)&nbsp;*&nbsp;cos(lat2*d2r)&nbsp;*&nbsp;pow(sin(dlong/2.0),&nbsp;2); &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;c&nbsp;=&nbsp;2&nbsp;*&nbsp;atan2(sqrt(a),&nbsp;sqrt(1-a)); &nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;d&nbsp;=&nbsp;3956&nbsp;*&nbsp;c;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;d; }
打开App,查看更多内容
随时随地看视频慕课网APP