计算2个GPS坐标之间的距离 [英] Calculate distance between 2 GPS coordinates

查看:877
本文介绍了计算2个GPS坐标之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

推荐答案

根据纬度和经度计算两个坐标之间的距离,包括一个Javascript实现。

Calculate the distance between two coordinates by latitude and longitude, including a Javascript implementation.

West South 位置为负值。
请记住,分钟和秒数都在60以内,所以S31 30'是-31.50度。

West and South locations are negative. Remember minutes and seconds are out of 60 so S31 30' is -31.50 degrees.

不要忘记将度数转换为弧度。许多语言都有这个功能。或者简单的计算:弧度=度* PI / 180

Don't forget to convert degrees to radians. Many languages have this function. Or its a simple calculation: 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;
}

以下是一些使用示例:

Here are some examples of usage:


distanceInKmBetweenCoordinates(0,0,0,0)//相同点之间的距离应为0
0
distanceInKmBetweenCoordinates(51.5,0,38.8, - 77.1)//从伦敦到阿灵顿
5918.185064088764

distanceInKmBetweenCoordinates(0,0,0,0) // Distance between same points should be 0 0 distanceInKmBetweenCoordinates(51.5, 0, 38.8, -77.1) // From London to Arlington 5918.185064088764

这篇关于计算2个GPS坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆