Monday, January 28, 2013

Calculando la distancia entre coordenadas utilizando Haversine en Java

Haversine Fomula

public static double toRad(double value)
{
   return value * Math.PI / 180;
}
 
public static double getDistanceM( double lat1, double lon1, double lat2, double lon2 )
{
final int R = 6371000; 
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;
return distance;
}

1 comment: