-->

didUpdateLocations代替didUpdateToLocation(didUpdateL

2019-06-18 08:16发布

随着iOS6的苹果的发布要我们用didUpdateLocations而不是didUpdateToLocation。 谁能解释如何正确使用didUpdateLocations?

Answer 1:

我asume你用下面的委托,以获得最后的位置?

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

委托以上在IOS 6.弃用现在应使用以下:

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

为了得到最后的位置,简单地获取数组的最后一个对象:

[locations lastObject]

换句话说, [locations lastObject]新的委托)等于newLocation (旧代表)。



Answer 2:

这里其他的答案中没有解释为什么有一个位置阵列和如何正确使用新didUpdateLocations:数组提供的。

弃用的目的locationManager:didUpdateToLocation:fromLocation:和发送位置一个NSArray而不是在后台运行时降低功耗。

与iPhone 5开始,GPS芯片具有存储一段时间的位置,然后在一个数组在一次解救他们所有的能力。 这就是所谓的延迟位置更新。 这使主CPU,而在后台的一段较长的时间睡觉。 这意味着iOS不必须开始为每个位置更新的主CPU,CPU可以睡觉,而GPS芯片集的位置。

您可以检查使用此功能deferredLocationUpdatesAvailable方法。 如果可以,你可以使用启用allowDeferredLocationUpdatesUntilTraveled:timeout:方法。 一些报考条件,看到这个答案的详细信息。



Answer 3:

它给你的对象数组访问最后一个位置,您可以使用

[locations lastObject]

从这个委托方法

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


Answer 4:

这是怎样的方法可以实现为已过时的一个类似工作

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = locations.lastObject;
    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = locations[locations.count - 2];
    }
}


Answer 5:

如果你支持的iOS 5和6,你应该叫

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

从旧

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

功能,通过构建的位置的阵列。



Answer 6:

值得注意的是,由返回的LocationManager CLLocation对象数组:didUpdateLocations:可以或可以不包括一个前位置。 换句话说,可能存在在少数情况下在阵列中只有一个位置。 使用以下方法来检查,如果有一个以上的对象,我们可以获取最新的前位置:

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

由于将有数组中的至少一个对象,获取当前位置通过请求数组中的最后一个对象来完成。 即使最后目标是唯一的对象,这是确定的:

CLLocation *newLocation = [locations lastObject];

请记住,因为这是一个数组,oldLocation在上面显示将不一定是你正在寻找的一个例子。 这取决于你如何setDistanceFilter:和desiredAccuracy:属性,这些属性会影响您的位置数组如何填充。 所需oldLocation可以埋入在阵列中更深。



文章来源: didUpdateLocations instead of didUpdateToLocation