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.