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;
}

No comments:

Post a Comment

SQLCIPHER PHP 7.4 UBUNTU

 This took me too much time to implement, after two hours of searching, I didnt found and documentation of how to compile sqlcipher support ...