2015-10-17 85 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

当然。实际上,代码本身解释了这一切。无论如何,你还想知道这个公式? –