首先仔细阅读问题...
我需要的是直线距离,而不是步行、汽车或其他方式。
请看下面给出的图像:
谷歌提供了汽车和驾车的距离。
但是我不想要这个,我想要两个位置之间的直线距离(纬度-经度)。
这在图中显示为红线。
注意:我不想在谷歌地图上放置红线,只想要单位距离(英里、千米等)的距离。
首先仔细阅读问题...
我需要的是直线距离,而不是步行、汽车或其他方式。
请看下面给出的图像:
谷歌提供了汽车和驾车的距离。
但是我不想要这个,我想要两个位置之间的直线距离(纬度-经度)。
这在图中显示为红线。
注意:我不想在谷歌地图上放置红线,只想要单位距离(英里、千米等)的距离。
安卓
double distance
Location locationA = new Location(“point A”)
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location(“point B”);
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
数学上
a = distance in degrees //meterConversion = 1609;
b = 90 - latitude of point 1
c = 90 - latitude of point 2
l = longitude of point 1 - longitude of point 2
Cos(a) = Cos(b)Cos(c) + Sin(b)Sin(c)Sin(l)
d = circumference of Earth * a / 360 // circumference of Earth = 3958.7558657440545D km
Haversine函数用于计算球面上两点之间的距离。
将其扩展到计算地球上两点之间的直线距离相对简单。尽管地球不是一个完美的球体,但使用赤道半径的标准测量(称为WGS84)仍然是一个很好的近似值。
如CommonsWare所说,您可以使用distanceBetween()来非常简单地实现这一点,该函数使用Haversine函数和WGS84半径。
要更好地理解实现/数学,请查看此Python示例代码。
使用以下代码可计算距离。只需获取两个geoPoint的纬度和经度,然后将其用于下面的计算中。
R = 6371; // km
d = Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * R;
这将是所有计算后返回的距离。
R是表面半径,以KM为单位,在计算中需要使用。您可以尝试使用此值。希望对您有用。