ios didUpdateLocations 而不是 didUpdateToLocation

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12602463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:24:20  来源:igfitidea点击:

didUpdateLocations instead of didUpdateToLocation

iosobjective-cswiftcore-locationcllocationmanager

提问by YogevSitton

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

随着 iOS6 的发布,Apple 希望我们使用 didUpdateLocations 而不是 didUpdateToLocation。谁能解释一下如何正确使用 didUpdateLocations?

回答by Anne

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:

上面的委托在 iOS 6 中已弃用。现在应该使用以下内容:

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

换句话说,[locations lastObject](new delegate) 等于newLocation(old delegate) 。

回答by progrmr

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.

这里的其他答案都没有解释为什么有一个 location 数组以及如何正确使用提供的新 didUpdateLocations: 数组。

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.

弃用locationManager:didUpdateToLocation:fromLocation:和发送位置的 NSArray的目的是减少在后台运行时的功耗。

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.

从 iPhone 5 开始,GPS 芯片能够将位置存储一段时间,然后以阵列形式一次性提供所有位置。这称为延迟位置更新。这允许主 CPU 在后台休眠更长时间。这意味着 iOS 不必每次更新位置都启动主 CPU,CPU 可以休眠,而 GPS 芯片会收集位置。

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

您可以使用该deferredLocationUpdatesAvailable方法检查此功能。如果可用,您可以使用allowDeferredLocationUpdatesUntilTraveled:timeout:方法启用它。某些条件适用,有关详细信息,请参阅此答案

回答by Janak Nirmal

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

回答by Groot

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

回答by Mustafa

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

如果您同时支持 iOS 5 和 6,则应致电

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

函数,通过构建位置数组。

回答by bitsand

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:

值得注意的是,CLLocation阵列对象通过的LocationManager返回:didUpdateLocations:可以或可以不包括一个前位置。换句话说,在极少数情况下,阵列中可能只有一个位置。使用以下检查,如果有多个对象,我们可以检索最近的先前位置:

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.

请记住,因为这是一个数组,所以上面显示的示例中的 oldLocation 不一定是您要查找的那个。这取决于您如何 setDistanceFilter: 和 desiredAccuracy: 属性,因为这些属性会影响您的位置数组的填充方式。所需的 oldLocation 可能会更深地埋在数组中。