0

i am using haversine formula to calculate distance but i am getting wrong distance actually google map distance is 8.1km but haversine formula is showing 4.06

private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM

public static double distance(double startLat, double startLong, double endLat, double endLong) {

    double dLat = Math.toRadians((endLat - startLat));
    double dLong = Math.toRadians((endLong - startLong));

    startLat = Math.toRadians(startLat);
    endLat = Math.toRadians(endLat);

    double a = haversin(dLat) + Math.cos(startLat) * Math.cos(endLat) * haversin(dLong);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return EARTH_RADIUS * c; // <-- d
}

public static double haversin(double val) {
    return Math.pow(Math.sin(val / 2), 2);
}
  • 1
    Your formula could be off, but first of all, is that Google Map's distance the point-to-point distance, or is it the distance travelled via streets/roads? Note that the two would likely not always be the same. – Tim Biegeleisen Apr 06 '18 at 08:29
  • 1
    Can you give inputs, expected output and actual output? Plus, shouldn't the earth radius be in meters instead of kilometers? – sp00m Apr 06 '18 at 08:30
  • 1
    @sp00m if an answer in kilometres is required, you need to have the Earth's radius in kilometres. If an answer in metres is required, you need to have the Earth's radius in metres. – Dawood ibn Kareem Apr 06 '18 at 08:32
  • Possible duplicate of [Calculate distance between two latitude-longitude points? (Haversine formula)](https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula) – JussiV Apr 06 '18 at 08:40
  • actually i am suing in km but also not working – Charankumar Mysore Apr 06 '18 at 08:41
  • inputs of two distance(17.451955, 78.478187, 17.442504, 78.441323); – Charankumar Mysore Apr 06 '18 at 08:42
  • hi JssiV, i used same but it also showing same wrong distance – Charankumar Mysore Apr 06 '18 at 08:45

1 Answers1

1

Your formula is correct, the problem come from bad use of google map

As suggested by Tim in comments, you need to see the point-to-point distance, not road distance

Using the two distance(17.451955, 78.478187, 17.442504, 78.441323) give 4.06km:

map

8.1km is probably the distance by road, and not point-to-point

Kepotx
  • 1,095
  • 12
  • 26
  • Don't you said that you had 4.06 km using the formula? – Kepotx Apr 06 '18 at 09:00
  • actually i need to get near restaurants based on save restaurants with lat and long in database with current lat and long..if i use google api for one rest lat and lon its fine,but if i need to compare 50 restaurants,it is not good to – Charankumar Mysore Apr 06 '18 at 09:00
  • when i am using above code i am getting different distance which is not not same distance google map – Charankumar Mysore Apr 06 '18 at 09:02
  • I just test it, java code give me 4.04925264532218. They are not *exact* same distances, but both google map and your code give us 4.05km, isn't that what you want? – Kepotx Apr 06 '18 at 09:20
  • hi Kepotx, actually distance is 8 km but showing 4.04km – Charankumar Mysore Apr 09 '18 at 11:47
  • point to point distance is 4.05km. If you want road distance, you can't use haversine formula – Kepotx Apr 09 '18 at 12:02