0

I'm trying to call a function in case the distance traveled since last checked is > 30 m. Based on the Class description of CLLocationDistance the value returned is in m.

I use this code:

[locationManager startUpdatingLocation];
if (!startingLocation)
    startingLocation = locationManager.location;
//   NSLog(@"Latitude:%f Longitude:%f", startingLocation.coordinate.latitude, startingLocation.coordinate.longitude);

    updatedLocation = locationManager.location;
CLLocationDistance distance = [updatedLocation distanceFromLocation:startingLocation];
if (distance > 30) {
    NSLog(@"%f",distance);
    NSLog(@"Latitude:%f Longitude:%f", updatedLocation.coordinate.latitude, updatedLocation.coordinate.longitude);
    [self stop];

The console output with NSLog returns 7946754.993111, and I did not even touch the phone. Any suggestions are appreciated!

coernel
  • 79
  • 1
  • 9

2 Answers2

2

8000 km between two points? I'll bet the first point is 0, 0 and you're about 8000 km from there, right?

In any case be sure to only use valid locations by checking to make sure the horizontalAccuracy is >=0 before using the location coordinates. Invalid locations have a negative horizontalAccuracy.

Also, you should probably discard any cached locations by checking the age of the location data and don't use it if it is more than a few seconds old.

See the sample code here that does these checks.

Another thing to watch out for is that you can get locations that show false motion due to changes in accuracy. See this answer for details.

Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
0

Is it possible that because you are calling startUpdatingLocation then getting that location then immediately asking for an update that the location is still trying to figure out exactly where it is, hence the big movement.

Try calling startUpdatingLocation from another method then implement the delegate method from CLLocationManager locationManager:didUpdateToLocation:fromLocation to get the movement.

Littlejon
  • 1,066
  • 9
  • 24