Get accurate Distance in Meters between two points on earth - Android android.location

Android examples for android.location:Distance

Description

Get accurate Distance in Meters between two points on earth

Demo Code

public class Main {

  private static final double EARTH_RADIUS_METERS = 6367000.0;

  public static double accurateDistanceMeters(double lat1, double lng1, double lat2, double lng2) {
    double dlat = Math.sin(0.5 * (lat2 - lat1));
    double dlng = Math.sin(0.5 * (lng2 - lng1));
    double x = dlat * dlat + dlng * dlng * Math.cos(lat1) * Math.cos(lat2);
    return (2 * Math.atan2(Math.sqrt(x), Math.sqrt(Math.max(0.0, 1.0 - x)))) * EARTH_RADIUS_METERS;
  }/*ww w .java  2  s.  c  om*/

}

Related Tutorials