didUpdateLocations instead of didUpdateToLocation

IosObjective CSwiftCore LocationCllocationmanager

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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) .

Solution 2 - Ios

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.

Solution 3 - Ios

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

Solution 4 - Ios

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];
    }
}

Solution 5 - Ios

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.

Solution 6 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYogevSittonView Question on Stackoverflow
Solution 1 - IosAnneView Answer on Stackoverflow
Solution 2 - IosprogrmrView Answer on Stackoverflow
Solution 3 - IosJanak NirmalView Answer on Stackoverflow
Solution 4 - IosGrootView Answer on Stackoverflow
Solution 5 - IosMustafaView Answer on Stackoverflow
Solution 6 - IosbitsandView Answer on Stackoverflow