计算2个GPS坐标之间的距离 [英] Calculate distance between 2 GPS coordinates
问题描述
我如何计算两个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屋!