Ios CLLocationManager异常行为
我有个奇怪的问题 我有两门课:Ios CLLocationManager异常行为,ios,objective-c,gps,location,cllocationmanager,Ios,Objective C,Gps,Location,Cllocationmanager,我有个奇怪的问题 我有两门课: 商业风险投资 偏爱 在这两种情况下,我都会向用户显示从他到某个特定点的当前距离。 在这两个类中,我使用了完全相同的代码 FavoritesVC作为模态视图控制器提供 出于某种原因,didUpdateLocations/DidUpdateLocation方法仅在BusinessVC中调用,而不是在FavoritesVC中调用 我正在两个类中实现CLLocationManagerDelegate委托 我正在使用的代码: -(void)viewDidLoad {
-(void)viewDidLoad {
[super viewDidLoad];
[self locatedMe];
}
-(void) locatedMe{
NSLog(@"locateMe");
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDistanceFilter:kCLDistanceFilterNone]; // whenever we move
[_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters]; // 100 m
[_locationManager startUpdatingLocation];
}
// For iOS6
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * newLocation = [locations lastObject];
NSNumber *lat = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
NSNumber *lon = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
self.userLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]];
}
// For earlier then iOS6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSNumber *lat = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
NSNumber *lon = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
self.userLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]];
NSLog(@"%@",self.userLocation);
}
好的!通过使用以下代码在主线程上运行代码修复了此问题:
-(void) locateMe {
NSLog(@"locateMe");
dispatch_async(dispatch_get_main_queue(), ^{
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // 100 m
[_locationManager startUpdatingLocation];
[_locationManager startUpdatingHeading];
});
}呼叫
[自动定位DME]view中的code>将出现在viewDidLoad
中。