I have set up the following method:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
It is working correctly and is being called, but it never contains more than one location. I am trying to get the distance between the new and old location when the phone moves. Am I missing something? There are never two items in the locations array.
As the documentation for didUpdateLocations says the array is ...
... An array of
CLLocationobjects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.
Generally you're right and there is often only one location, so you should keep track of your own property where you'll store the last valid location you received from the last time didUpdateLocations was called.
You're doing this right. Usually, locationManager:didUpdateLocations: will have only one location, especially when your app is foregrounded, location services have been recently active, etc.
It can, though, have multiple locations - typically in less usual circumstances, e.g. if your app was started for a background location update and additional ones come in while it is starting up.
So don't sweat it. But in any case, you'll want to keep track of the 'old' location yourself - it's not the case that the array would contain historical locations that happen to be the ones you want. Rather, it's more likely to be 'stale' data that may suffice in some circumstances, or stacked up updates.
Added:
To keep track of how the phone is moving, you'll want to keep your own CLLocation variable in the class where you're tracking location. You will need to determine what business logic you want to implement. If you want literally the distance between every update and the last one, just add a variable called lastLocation and use CLLocation's distanceFromLocation: to get the difference. (See the docs at Apple's developer site for more info.)
But I doubt that's quite what you want. Then you may get 1 meter each time, and never really know if you've gone 1000 meters in one direction. . . . or bounced back and forth and stayed in the same spot.
Probably you'll need to have some sort of minimum timestamp - so when a new update comes in, compare theNewLocation.timestamp to theOldLocation.timestamp and only measure the distance if, say, it's been at least ten minutes.
(If you do something longer than a few seconds, you're best off turning off location updates in between.)
Or, perhaps, there is some other logic you want to use. For example, getting the distance only when the new location is a different city than the previous one, or adding the new location to an array when it's finally at least 500 meters from the old one, or etc. This, too, falls under depending heavily on what you want to do with the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With