2015-10-17 82 views
1

在Android應用程序中,我遇到了一個問題,任何人都有關於GPS座標的信息,請幫助。使用谷歌地圖api如何cmpare兩個位置的GPS座標(latti,longi)

的問題是,如果我有地圖和兩個不同的點

我想檢查點在地圖上 相同的位置座標(經度和緯度)其次是兩個點更接近彼此根據任何標準

例如..如果點在另一點的100米半徑它似乎更接近。

+0

Location1.distanceTo(location2) – cYrixmorten

回答

0

您可以使用haversine公式來計算兩個點,

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    final int R = 6371; // Radious of the earth 
    Double lat1 = Double.parseDouble(args[0]); 
    Double lon1 = Double.parseDouble(args[1]); 
    Double lat2 = Double.parseDouble(args[2]); 
    Double lon2 = Double.parseDouble(args[3]); 
    Double latDistance = toRad(lat2-lat1); 
    Double lonDistance = toRad(lon2-lon1); 
    Double a = Math.sin(latDistance/2) * Math.sin(latDistance/2) + 
       Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
       Math.sin(lonDistance/2) * Math.sin(lonDistance/2); 
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    Double distance = R * c; 

    System.out.println("The distance between two lat and long is::" + distance); 

} 

您可以比較這distance.This會做你的工作。

+0

感謝您的回答...我得到了您的建議...並且它似乎是解決方案... 但我對這個公式有點困惑...你能告訴我關於沙丁胺配方 –

+0

當然。實際上,代碼本身解釋了這一切。無論如何,你還想知道這個公式? –