-3

I'm developing an app that uses GPS position. For knowing position I get latitude and longitude.

I don't get position at regular intervals, but I want to know if user has moved. For that I store last position and current position (both latitude and longitude)

I was doing this:

if (latitudNew != latitudOld && longitudNew != longitudNew)
{
    float R = 6378.137;
    double dLat = ((latitudNew - latitudOld) * M_PI) / 180;
    double dLong = ((longitudNew - longitudOld) * M_PI) / 180;

    double a  = sin(dLat/2) * sin(dLat/2) + cos((latitudOld*M_PI)/180) * cos((latitudNew*M_PI)/180) * sin(dLong/2) * sin(dLong/2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double d = R * c;
}

Where distance is stored in d.

My problem is that it never get past the if condition, so I know I'm not comparing it correctly.

I can't use this solution, as I can have negative numbers (one or both or none).

Any ideas??

Community
  • 1
  • 1
Sascuash
  • 3,661
  • 10
  • 46
  • 65
  • 6
    Typo - `longitudNew != longitudNew` will normally always be false. – rmaddy May 01 '15 at 17:16
  • Also, do you need to bother comparing them? Just calculate distance and if it's more than x meters, then "user has moved". Use the built-in distanceFromLocation method instead of manual calculation. –  May 01 '15 at 17:19

3 Answers3

3

Comparing doubles isn't a good idea. What you'll need to do is to subtract the two values, then check if the difference between the two is greater than a threshold value you'll need to define, like 0.0001.

joels
  • 1,292
  • 15
  • 21
  • While this is probably a good idea it has nothing to do with the issue here. – rmaddy May 01 '15 at 17:17
  • I noticed your comment on "longitudNew != longitudNew". I overlooked this. True it will nearly always be false. – joels May 01 '15 at 17:23
  • 1
    There's a constant `DBL_EPSILON` (and corresponding `FLT_EPSILON` for `float`s) intended for the minimum difference value you mentioned. – Tim Johnsen May 01 '15 at 18:07
3

You have the line:

if (latitudNew != latitudOld && longitudNew != longitudNew)

This is doing longitudNew != longitudNew. You have a typo there, you're testing to see if a variable is not equal to itself. It'll always be false. You meant to put longitudOld for one of them. Though you probably want to replace the && with ||, because you want to execute the code in the if block if either of these conditions is true.

Also, this is a bit of a nit... The proper spellings are longitude and latitude. You left the e off of both.

Gavin
  • 8,204
  • 3
  • 32
  • 42
2

That looks correct to me, you'd compare doubles in Objective-C just as you would in C. The datasource you're getting the information from might be rounding the values so that they appear as equal or the data isn't updating often enough.

Also, you'd probably want to change the logic to have an || in the if instead of an &&. You'd still consider an update in location to be an update, even if it was just in one of the two.

Tim Johnsen
  • 1,471
  • 14
  • 33
  • 1
    It isn't correct. You'll notice if you look again that there's a typo. Check out my answer to see also. – Gavin May 01 '15 at 17:23