距离从两个地方计算

时间:2010-10-18 12:05:21

标签: android

我有两个地方的纬度和经度。我可以在android中计算它们之间的距离吗?

3 个答案:

答案 0 :(得分:4)

您正在寻找的是半正式公式,它使用纬度和经度给出球体上两点之间的距离。

Haversine Formula:

R =地球半径(平均半径= 6371km)
Δlat= lat2 - lat1
Δlong= long2 - long1
a =sin²(Δlat/ 2)+ cos(lat1)* cos(lat2)*sin²(Δlong/ 2)
c = 2 * atan2(√a,√(1-a))
d = R * c

在JavaScript中:

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

http://www.movable-type.co.uk/scripts/latlong.html

答案 1 :(得分:3)

答案 2 :(得分:3)

此API是您想要的:

http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,%20double,%20double,%20double,%20float[])

(注意:它是静态的......不需要位置对象实例)