1

我正在使用 CoreLocation 框架在我的项目中获取纬度和经度。

我已经在我的 .m 文件中实现了这个委托方法

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

   NSLog(@"Locations : %@", locations); 
}

但是在 iOS 5.0 & 5.1 中,这个方法是不调用的。在 iOS 6.0 或更高版本中,它可以正常调用。那么如何在 iOS 5.0 或更高版本中调用此方法。

4

4 回答 4

2

didUpdateLocations:在 ios 5 或更早版本中不存在。如果您想支持那些 ios 版本,则只需使用已弃用的didUpdateToLocation:fromLocation. 即使它已被弃用,它仍然适用于 ios 6 和 7。这是最简单的解决方案。

您可以为这两种方法设置委托方法,但管理起来会更加复杂。如果您不需要在后台构建跟踪日志,那么您实际上不需要didUpdateLocations: 创建它来节省电池电量,同时在后台运行时收集多个点。

于 2013-07-23T14:40:34.357 回答
0
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

上述委托方法在 iOS 6 中已弃用。

而不是这个,使用:

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

此代码(以及 pausesLocationUpdatesAutomatically)只能在 XCode 4.5(其中基础 SDK 为 6)中编译。

如果您想定位 6.0 之前的 iOS 版本,请使用此宏

#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

无论您在哪里创建CLLocationManager对象

if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO (@"6.0"))
{
    locationManagerObj.pausesLocationUpdatesAutomatically = NO;
}

更多细节在这里:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

于 2013-07-23T07:36:46.893 回答
0

请参阅此答案...此方法适用于 iOS 5(在 iOS 6 上已弃用)和旧版本。

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

此方法适用于 iOS 6 及更高版本..

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations
于 2013-07-23T05:24:46.977 回答
0

参考:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html

如您所见locationManager:didUpdateLocations:,仅在iOS 6.0及以后可用。

您始终可以从以下位置获取最近更新的位置

CLLocationManger's location 

财产。

于 2013-07-23T05:24:56.640 回答