【问题标题】:iphone: CLLocation manager delegate method is not called on click of a buttoniphone:单击按钮时不调用 CLLocation 管理器委托方法
【发布时间】:2011-05-27 10:52:07
【问题描述】:

再糟糕的一天,上帝知道这是什么问题,但是一旦我单击刷新按钮,就不会调用 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];

} 请帮我解决这个问题。

提前致谢

【问题讨论】:

    标签: iphone cocoa-touch cllocationmanager


    【解决方案1】:

    它似乎在viewWillAppear 上开始更新,所以它不会更新位置,直到它没有被stopUpdatingLocation 停止。如果你想重新开始更新,你应该在startUpdatingLocation之前调用stopUpdatingLocation

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

    恕我直言,位置更新一般不需要重启

    【讨论】:

    • 是的,这该死的没用。如果没有必要,为什么建议它?
    • 我无法想象为什么要重新启动更新,但提供的示例代码背后可能有一些逻辑? :)
    【解决方案2】:

    您误解了CLLocationManager 的工作原理。当CLLocationManager 为您提供新位置时,将调用委托。 [locationManager startUpdatingLocation]; 只是告诉位置管理器开始更新位置,所以不需要调用两次。

    编辑:为了清楚起见:当位置更新时调用委托,即当设备移动时。

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 2013-05-20
      • 1970-01-01
      • 2014-08-23
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多