iphone:单击按钮时不调用CLLocation管理器委托方法

时间:2011-05-27 10:52:07

标签: iphone cocoa-touch cllocationmanager

另一个糟糕的一天上帝知道这个问题但是一旦我点击刷新按钮就没有调用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];

} 请帮我解决这个问题。

提前致谢

2 个答案:

答案 0 :(得分:1)

它似乎已开始在viewWillAppear上更新,因此在stopUpdatingLocation未停止之前,它不会更新位置。如果您想重新开始更新,请在stopUpdatingLocation之前致电startUpdatingLocation

-(IBAction) refrechLoc{
    NSLog(@"updating location");
    [locationManager stopUpdatingLocation];
    [locationManager startUpdatingLocation];
}

imho,一般情况下无需重新启动位置

答案 1 :(得分:0)

你误解了CLLocationManager是如何运作的。当CLLocationManager为您准备了新位置时,系统会调用该代理人。 [locationManager startUpdatingLocation];只是告诉位置管理员开始更新位置,因此无需再拨打两次。

编辑:为清晰起见:在更新位置时,即设备移动时,会调用代理。