Skip to content

Instantly share code, notes, and snippets.

@theresajayne
Created October 2, 2013 09:26
Show Gist options
  • Save theresajayne/6791210 to your computer and use it in GitHub Desktop.
Save theresajayne/6791210 to your computer and use it in GitHub Desktop.
public double calculateDistance(double lat1,double lng1, double lat2, double lng2)
{
double RADIUS_OF_EARTH = 3958.75;
lat1 = Math.toRadians(lat1);
lng1 = Math.toRadians(lng1);
lat2 = Math.toRadians(lat2);
lng2 = Math.toRadians(lng2);
double dlon = lng2 - lng1;
double dlat = lat2 - lat1;
double a = Math.pow((Math.sin(dlat/2)),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = RADIUS_OF_EARTH * c;
return dist;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment