繁体   English   中英

如何在地图中找到两个地理位置之间的正确距离?

[英]how to find the correct distance between two geopoints in map?

我需要开发应用程序,用户必须找到他停放的汽车并显示他和停车之间的距离。我使用GPS和定位服务。

对于距离我用haversine公式但距离始终显示0米。

我尝试了很多搜索谷歌的解决方案,但dint得到任何正确的解决方案。

谁能提出他们的建议?

Google文档有两种方法

在此输入图像描述

如果你从GeoPoint获得lat / lon,那么他们是微观的。 你必须乘以1e6。

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

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/

尝试在android.location API中使用This方法

distanceBetween(double startLatitude,double startLongitude,double endLatitude,double endLongitude,float [] results)

该方法计算两个位置之间以米为单位的近似距离,并且可选地计算它们之间的最短路径的初始和最终方位

注意:如果你从GeoPoint获得lat / lon,那么他们是微观的。 你必须乘以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;
   }
}

distanceBetween()方法将为您提供两点之间的直线距离。 得到两点之间的路线距离看到我的ansewer 这里

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

Geopoints有getLongitudeE6()和getLatitudeE6()来帮助。 请记住,那些是E6所以你需要除以1E6。

harvesine公式的问题在于它不计算实际距离。 它是球体上2个点的距离。 真正的距离取决于街道或水路。 Harvesine公式也有点复杂,因此更容易让Google-Api给出真正的距离。 使用Googlemaps Api,您需要了解api的路线。

distanceBetween不会影响实际距离(道路距离)所以我建议你访问这个谷歌源代码,它会显示你2个地理点之间的真实道路距离。 链接有两个版本一个用于Android和一个用于黑莓检查出来

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM