didUpdateLocations instead of didUpdateToLocation

强颜欢笑 提交于 2019-12-28 03:31:10

问题


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


回答1:


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




回答2:


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.




回答3:


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



回答4:


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



回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/12602463/didupdatelocations-instead-of-didupdatetolocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!