Search code examples
javaandroidgoogle-mapslocationgeopoints

Location and GeoPoint distanceTo()


I have location of user(class Location). And list of GeoPoints. Is it some function to calculate distance? Or write own function it is the only solution?


Solution

  • I believe the easiest way would be to use the distanceBetween function in the Android Location class.

    Location userLocation = new Location();
    GeoPoint geoPoint = new GeoPoint();
    float[] results = new float[3];
    Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), (geoPoint.getLatitudeE6()/1E6), (geoPoint.getLongitudeE6()/1E6), results); 
    //result[0] = distance in m