how to find the distance between two geopoints?

后端 未结 6 1956
遇见更好的自我
遇见更好的自我 2020-12-06 01:55
double distance;  

Location locationA = new Location(\"point A\");  

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

Location locationB = new Locat         


        
6条回答
  •  没有蜡笔的小新
    2020-12-06 02:13

    Just a quick snippet since I didn't see a complete and simple solution with GeoPoints above:

    public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
        double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
        double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
        double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
        double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
        float [] dist = new float[1];
        Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
        return dist[0] * 0.000621371192f;
    }
    

    If you want meters, just return dist[0] directly.

提交回复
热议问题