simplify haversine formula

This commit is contained in:
sonora 2013-06-18 11:00:26 +02:00
parent e6a8895f42
commit addc32965a

View file

@ -76,14 +76,17 @@ public class MapUtils {
* Gets distance in meters * Gets distance in meters
*/ */
public static double getDistance(double lat1, double lon1, double lat2, double lon2){ public static double getDistance(double lat1, double lon1, double lat2, double lon2){
double R = 6371; // km double R = 6372.8; // for haversine use R = 6372.8 km instead of 6371 km
double dLat = toRadians(lat2-lat1); double dLat = toRadians(lat2-lat1);
double dLon = toRadians(lon2-lon1); double dLon = toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) + double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2); Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); //double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c * 1000; //return R * c * 1000;
// simplyfy haversine:
double c = 2 * R * 1000 * Math.asin(Math.sqrt(a));
return c;
} }