计算 2 个 GPS 坐标之间的距离 [英] Calculate distance between 2 GPS coordinates
本文介绍了计算 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;
}
以下是一些用法示例:
distanceInKmBetweenEarthCoordinates(0,0,0,0) // Distance between same
// points should be 0
0
distanceInKmBetweenEarthCoordinates(51.5, 0, 38.8, -77.1) // From London
// to Arlington
5918.185064088764
这篇关于计算 2 个 GPS 坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文