0

I have code which works in India well. This code calculate the approximate distance between two locations. I used the code mentioned below. But If i run this same code in USA. Then for every location updates calculates the different distance. I passed the source and destination latitude and longitude to this function.The app works well in India. But not works in USA. At same place it shows different calculations. Thanks in advance.Any help will be appreciate.

public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = Math.toRadians(lat1);
        double lat2Rad = Math.toRadians(lat2);
        double deltaLonRad = Math.toRadians(lon2 - lon1);
        return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                * Math.cos(deltaLonRad))
                * 6371;
    }  
silvia_aut
  • 1,481
  • 6
  • 19
  • 33
  • try double distance = FIRSTLOCATION.distanceTo(SECONDLOCATION); – Breadbin Nov 13 '13 at 14:02
  • there are probably known limitations with this formula. can you explain a/ a little more about it. b/ what are your expected vs actual results for some example measurements both in india and in the USA. – njzk2 Nov 13 '13 at 14:02
  • (also, there are built-in android methods for doing that) – njzk2 Nov 13 '13 at 14:03
  • @njzk2 Thanks for your quick reply.Details are India CurrentLat :18.50849876 CurrentLon:73.79455861 My home dist:743 next onlocation changed : CurrentLat :18.50846096 CurrentLon:73.79453276 My home dist:701 location changes after 3 seconds and 1meter. requestonLocationUpdates – user2861895 Nov 13 '13 at 14:18
  • Please post the numbers for lat1,lon1, lat2,lon2 and the distance you get whoich you think is wrong. Probaly your formula works. Ente r them at Google Earth use the distance ruler tool. – AlexWien Nov 13 '13 at 18:25
  • @AlexWien thanks for your help. Lat1=33.963477 Lon1=-118.34549605 Lat2=33.9858518 Lon2=-118.3431153 I got the distnce as in feet 56.753101035336925 and after some seconds i got 8250.530050914727 feets. it is changed every onLocationChanges. but the same formula runs in India.Please help me out. – user2861895 Nov 14 '13 at 06:41
  • It is not the code of formula, it is your other code which delivers wrong, old, or unsuitable coordinates. – AlexWien Nov 14 '13 at 18:58

2 Answers2

0

The formula is the greater circle formula.
It is well known, that it does not work well for small distances.
The reason is this part:

Math.cos(deltaRad).  

This is a cosinus for a very small value, which is numerically "ill conditioned": They do not work well on computers. (Nowadays with double precision the effect is less visible) Therfore for small distances the haversine formula is better suited.

If you still would have problems with the haversine distance formula, then obviously your input data is the problem. (e.g if read from GPS without proper pre filtering)

The chance is high that your implemetation although using greater circle variant delivers correct result. Chances are high that your claimed "correct distance" is wrong.

Community
  • 1
  • 1
AlexWien
  • 28,470
  • 6
  • 53
  • 83
-1

I am using a very similar formula, and never had any complaints. The main difference in my formula is the deltaLonRad, which I calculate as follows:

double deltaLonRad = Math.toRadians(lon2) - Math.toRadians(lon1);
user2808624
  • 2,502
  • 14
  • 28
  • thanks for your reply, But is it working in any country? I mean are you sure? – user2861895 Nov 13 '13 at 14:14
  • Pretty sure. I am using it in an app since about 2 years. Just try with your examples from US which went wrong with your code. I guess the negative value of longitude in the US causes the problems. – user2808624 Nov 13 '13 at 14:17
  • I will change and test it then will let you know. – user2861895 Nov 13 '13 at 14:27
  • Your variant is mathematicaly equivalent. f*a - f*b = f*(a-b); to radians is only a factor. (180.0/ Pi) So your answer is not the cause of his problem. – AlexWien Nov 13 '13 at 18:09
  • I was not sure, whether the 360 degree "modulo" behavior also survives this equivalence. But you are right, its the same for 2*Pi after the transformation. I think the question was already misleading. It is not really related to coordinates in India versus coordinates in US, but has to do with very small coordinate diffs over time. (As far as I got the comments added later.) – user2808624 Nov 14 '13 at 07:07