66

With the release of iOS6 Apple wants us to use didUpdateLocations instead of didUpdateToLocation. Can anyone explain how to properly use didUpdateLocations?

4

6 回答 6

126

I asume you used the following delegate to get the last position?

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation

The delegate above is deprecated in iOS 6. Now the following should be used:

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations

In order to get the last position, simply get the last object of the array:

[locations lastObject]

In other words, [locations lastObject] (new delegate) equals newLocation (old delegate) .

于 2012-09-26T13:47:24.637 回答
65

None of the other answers here explain why there is a locations array and how to properly use the new didUpdateLocations: array that is provided.

The purpose of deprecating the locationManager:didUpdateToLocation:fromLocation: and sending an NSArray of locations instead is to reduce power consumption when running in the background.

Starting with iPhone 5, the GPS chip has the capability to store locations for a period of time and then deliver them all at once in an array. This is called deferred location updates. This allows the main CPU to sleep for a longer period of time while in the background. It means iOS doesn't have to start the main CPU for every position update, the CPU can sleep, while the GPS chip collects locations.

You can check for this capability using the deferredLocationUpdatesAvailable method. If available you can enable it using allowDeferredLocationUpdatesUntilTraveled:timeout: method. Some conditions apply, see this answer for details.

于 2013-07-23T17:49:11.257 回答
22

It gives you array of objects to access last location you can use

[locations lastObject]

from this delegate method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
于 2012-09-26T13:48:32.540 回答
7

This is how the method can be implemented to work similarly as the deprecated one

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = locations.lastObject;
    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = locations[locations.count - 2];
    }
}
于 2014-09-09T08:16:57.663 回答
3

If you support both iOS 5 and 6, you should call

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations, 

from the older

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

function, by building an array of locations.

于 2012-10-12T18:04:18.940 回答
2

It is noteworthy that the array of CLLocation objects returned by locationManager:didUpdateLocations: may or may not include a former location. In other words, there may be on rare occasions only one location in the array. Use the following to check, and if there is more than one object we can retrieve the most recent prior location:

int objCount = [locations count];
if (objCount > 1) {
    CLLocation *oldLocation = [locations objectAtIndex:objCount - 1];
}

As there will be at least one object in the array, retrieving the current location is done by requesting the last object in the array. Even if the last object is the only object, that's Ok:

CLLocation *newLocation = [locations lastObject];

Keep in mind that because this is an array, oldLocation in the example shown above will not necessarily be the one you're looking for. This depends on how you setDistanceFilter: and desiredAccuracy: properties, as those properties will impact how your locations array is populated. The desired oldLocation may be buried deeper in the array.

于 2014-05-05T23:28:25.077 回答