StartMonitoringSignificantLocationChanges没有调用didupdateLocations

时间:2014-06-15 16:23:53

标签: ios objective-c ios7 cllocationmanager

我正在开发一个使用位置的iOS 7应用。我永远不会更新位置。如果我对startUpdatingLocation做同样的事情,那就行得很好。我已阅读文档,我不知道问题出在哪里。 该应用程序的位置已启用,它会在您第一次运行应用程序时询问您是否要启用它。此外,状态栏中的位置图标也未显示。

更新:在它运行的模拟器中,它给了我一个更新的位置。为什么它在设备中不起作用?

它只会在课堂上使用,这是配置。

#import <CoreLocation/CoreLocation.h>

@interface ClassLocation : UIViewController <CLLocationManagerDelegate>{
} 

在.h中设置属性,在.m中设置合成

@property (strong, nonatomic) CLLocationManager *localizacionManager;

@synthesize localizacionManager;

viewDidLoad中:

localizacionManager=[[CLLocationManager alloc] init];
[localizacionManager setDelegate:self];
[localizacionManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
[localizacionManager startMonitoringSignificantLocationChanges];

两个代表方法:

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"Location updated");
}

2 个答案:

答案 0 :(得分:4)

在Info.plist中添加一个密钥,并向位置管理员请求授权启动。

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

您需要请求相应位置方法的授权。

[self.locationManager requestWhenInUseAuthorization]

[self.locationManager requestAlwaysAuthorization]

答案 1 :(得分:1)

在iOS 7中我也是如此。我认为这是由于最后一个位置被缓存;如果当前位置与最后一个位置相比没有显着变化,则委托人永远不会被调用。

您可以使用CLLocationManager的“location”属性访问上一个缓存的位置:

@property (readonly, nonatomic, copy) CLLocation *location ;

希望这有帮助。