【问题标题】:CLLocationManager didUpdateLocations or didUpdateToLocationCLLocationManager didUpdateLocations 或 didUpdateToLocation
【发布时间】:2015-07-27 07:13:13
【问题描述】:

我的 iOS 我正在使用CLLocationManager,但我不确定使用哪种委托方法来获取位置更新。有两种方法

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

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

我知道我应该使用第二个,因为第一个已弃用,但我已将我的应用程序的部署目标设置为 6.0。那么我应该使用哪一个?在所附的图片中,就在这个方法的旁边

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

上面写着6.0。那么它在iOS 6.0 中已弃用或在 6.0 之前可用是什么意思?我的猜测是,我应该使用

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

应该没问题。有什么建议么?

【问题讨论】:

  • 您仍然支持 iOS 6.x 的任何具体原因?

标签: ios core-location cllocationmanager deployment-target


【解决方案1】:

我在旧项目中也遇到了同样的问题,所以我尝试了以下方法,然后对我来说工作正常

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

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

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

为了获取最后的位置,只需获取数组的最后一个对象:

[locations lastObject]

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

例子

iOS6 及以上

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count >= 2) {
    oldLocation = [locations objectAtIndex:locations.count-1];
} else {
    oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);

}

如果你在 iOS6 及以下使用也添加以下方法

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self locationManager:locationManager didUpdateLocations:[[NSArray alloc] initWithObjects:newLocation, nil]];
}

更多参考请关注tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多