簡體   English   中英

如何在地圖上顯示兩個地理位置之間的距離?

[英]how to display on map the distance between two geopoints?

我正在開發一個應用程序,用戶必須在其中記錄位置然后定位汽車。當他選擇“定位汽車”選項時,應該顯示停放的汽車。我計算了距離,但距離始終顯示為0米。任何人都告訴如何在地圖上顯示人與車之間的距離...我想在地圖上顯示所停的汽車

您保存了停放的汽車的位置? 你有這個人的實際位置嗎? 因此,那么您應該能夠計算出差異並至少獲得距離(至少是可見線的距離)...

如果您擁有USER和停放的汽車的位置...

那么這應該工作

Location locationA = new Location("USER");  

locationA.setLatitude(latA);  
locationA.setLongitude(lngA);  

Location locationB = new Location("Parked Car");  

locationB.setLatitude(latB);  
LocationB.setLongitude(lngB);  

distance = locationA.distanceTo(locationB); 

使用此函數來計算實際距離:

public static double distFrom (double lat1, double lng1, double lat2, double lng2 ) 
{
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return new Double(dist * meterConversion).doubleValue();
}

並像這樣使用它:

double distance = 0;
distance=distFrom(latA,lngA,latB,lngB);

如果要將雙精度型轉換為整數,請使用以下命令:

int distance=0;
distance=(int) distFrom(latA,lngA,latB,lngB);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM