From addc32965a1655dbf34762b44d2f96cb07f038c7 Mon Sep 17 00:00:00 2001 From: sonora Date: Tue, 18 Jun 2013 11:00:26 +0200 Subject: [PATCH] simplify haversine formula --- OsmAnd-java/src/net/osmand/util/MapUtils.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/MapUtils.java b/OsmAnd-java/src/net/osmand/util/MapUtils.java index 8a0224f927..0651d4393c 100644 --- a/OsmAnd-java/src/net/osmand/util/MapUtils.java +++ b/OsmAnd-java/src/net/osmand/util/MapUtils.java @@ -76,14 +76,17 @@ public class MapUtils { * Gets distance in meters */ 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 dLon = toRadians(lon2-lon1); double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2); - double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); - return R * c * 1000; + //double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + //return R * c * 1000; + // simplyfy haversine: + double c = 2 * R * 1000 * Math.asin(Math.sqrt(a)); + return c; }