簡體   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