我有一个奇怪的问题。
我有 2 节课:
- 商业风险投资
- 收藏夹VC
在他们两个中,我都向用户展示了从他到某个特定点的当前距离。在这两个类中,我都使用完全相同的代码。
FavoritesVC 以模态视图控制器的形式呈现。
出于某种原因,didUpdateLocations / didUpdateToLocation方法仅在 BusinessVC 中调用,而不在 FavoritesVC 中调用。
我在两个类中都实现了CLLocationManagerDelegate委托。
我正在使用的代码:
-(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);
}