如何在地图上找到两个地理点之间的正确距离?

5
我需要开发一个应用程序,用户需要定位他停放的车辆,并显示他与停放的车辆之间的距离。我使用了GPS和位置服务。为了计算距离,我使用了哈弗赛恩公式,但是距离总是显示为0米。我在谷歌上尝试了很多搜索解决方案,但没有找到正确的解决方案。请问有人可以提供建议吗?

重复的问题:https://dev59.com/f3E85IYBdhLWcg3wgDzd - logcat
阅读此问题的所有答案https://dev59.com/c1zUa4cB1Zd3GeqPzAij#7752341 - MKJParekh
请查看此处的问题:https://dev59.com/uWIk5IYBdhLWcg3wfOSf。其中有多个距离计算的类似答案,请查看被接受的答案。 - Aishvarya Jaiswal
6个回答

12

谷歌文档有两种方法

enter image description here

如果从GeoPoint获取lat / lon,则它们以微度为单位。 您必须乘以1e6。

但我更喜欢使用以下方法。(基于Haversine公式)

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

double dist = GeoUtils.distanceKm(mylat, mylon, lat, lon);

 /**
 * Computes the distance in kilometers between two points on Earth.
 * 
 * @param lat1 Latitude of the first point
 * @param lon1 Longitude of the first point
 * @param lat2 Latitude of the second point
 * @param lon2 Longitude of the second point
 * @return Distance between the two points in kilometers.
 */

public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
    int EARTH_RADIUS_KM = 6371;
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM;
}

最后,我想分享一些额外的信息。

如果你正在寻找驾车路线、两个地点之间的路线,请前往

http://code.google.com/p/j2memaprouteprovider/


@VipulShah - 我认为这只是两个GeoPoints之间的直线距离,而不是方向或驾车距离,对吗? - user370305
@user370305 太棒了!如果您需要驾驶距离和路线,请参阅http://code.google.com/p/j2memaprouteprovider/。 - Vipul
@vipul,但在Android应用程序中通常显示的距离是什么?直线/驾车? - Archie.bpgc
@Archie.bpgc 我认为这很简单...你可能有选择查看驾车距离的选项。 - Vipul

3

distanceBetween()方法可以给出两点之间的直线距离。如需获取两点之间的路程距离,请参考我的答案这里


3
尝试在android.location API中使用此方法: distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)。该方法可以计算两个位置之间的大致距离(以米为单位),还可以选择计算它们之间最短路径的初始和最终方向角。
注意:如果您从GeoPoint获取纬度/经度,则它们是微度。您必须乘以1E6。
如果您想通过Haversine公式计算2个Geopoint之间的距离,请使用此方法。
public class DistanceCalculator {
   // earth’s radius = 6,371km
   private static final double EARTH_RADIUS = 6371 ;
   public static double distanceCalcByHaversine(GeoPoint startP, GeoPoint endP) {
      double lat1 = startP.getLatitudeE6()/1E6;
      double lat2 = endP.getLatitudeE6()/1E6;
      double lon1 = startP.getLongitudeE6()/1E6;
      double lon2 = endP.getLongitudeE6()/1E6;
      double dLat = Math.toRadians(lat2-lat1);
      double dLon = Math.toRadians(lon2-lon1);
      double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
      Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
      Math.sin(dLon/2) * Math.sin(dLon/2);
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      return EARTH_RADIUS * c;
   }
}

1

android.location.Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

地理点有getLongitudeE6()和getLatitudeE6()来帮助。请记住,这些是E6,因此您需要除以1E6。


1
Harvesine公式的问题在于它不能计算真实距离,只能计算球面上两点之间的距离。真实距离取决于街道或水路。而且Harvesine公式有些复杂,因此更容易向Google-Api询问真实距离。使用Googlemaps Api需要学习方向API。

0

distanceBetween不是真实距离(路程距离)。因此,我建议您访问此Google源代码,它将向您显示两个地理点之间的真实道路距离。 链接Android黑莓两个版本,请查看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接