另一个糟糕的一天上帝知道这个问题但是一旦我点击刷新按钮就没有调用CLLocation Mangaer的委托方法,点击这个按钮我要求位置管理员更新位置
-(void) viewWillAppear:(BOOL)animated{
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"failed");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"current lat= %f and long=%f ", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 10) {
[lat setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]];
[lon setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]];
}
else {
[lat setText:@"Not changed"];
[lon setText:@"Not changed"];
}
}
-(IBAction) refrechLoc{
NSLog(@"updating location");
[locationManager startUpdatingLocation];
} 请帮我解决这个问题。
提前致谢
答案 0 :(得分:1)
它似乎已开始在viewWillAppear
上更新,因此在stopUpdatingLocation
未停止之前,它不会更新位置。如果您想重新开始更新,请在stopUpdatingLocation
之前致电startUpdatingLocation
:
-(IBAction) refrechLoc{
NSLog(@"updating location");
[locationManager stopUpdatingLocation];
[locationManager startUpdatingLocation];
}
imho,一般情况下无需重新启动位置
答案 1 :(得分:0)
你误解了CLLocationManager
是如何运作的。当CLLocationManager
为您准备了新位置时,系统会调用该代理人。 [locationManager startUpdatingLocation];
只是告诉位置管理员开始更新位置,因此无需再拨打两次。
编辑:为清晰起见:在更新位置时,即设备移动时,会调用代理。