Iphone上的locationManager DidUpdateLocation不一致

Iphone上的locationManager DidUpdateLocation不一致,iphone,cllocationmanager,Iphone,Cllocationmanager,第一次,我运行update_location例程,位置管理器快速跳转到didupdatetolocation的回调例程。没问题 但是,下次我再次调用update_location函数时,回调didupdatetolocation从未进入。为什么会有这样的差异?为什么不输入回调?您对LocationManager的使用不正确。您只需要调用startUpdatingLocation一次,只要有足够大的更改,就会重复调用回调 至于您观察到的原因,调用回调可能没有重大变化。为什么要创建一个计时器,不断调

第一次,我运行update_location例程,位置管理器快速跳转到didupdatetolocation的回调例程。没问题


但是,下次我再次调用update_location函数时,回调didupdatetolocation从未进入。为什么会有这样的差异?为什么不输入回调?

您对LocationManager的使用不正确。您只需要调用startUpdatingLocation一次,只要有足够大的更改,就会重复调用回调


至于您观察到的原因,调用回调可能没有重大变化。

为什么要创建一个计时器,不断调用CLLocationManager startUpdatingLocation

CLLocationManager通常通过将自己设置为代理来使用。启动它,然后在位置更改时收到回调

@implementation MyLocation


SYNTHESIZE_SINGLETON_FOR_CLASS(MyLocation);

@synthesize delegate, locationManager;

- (id) init 
{
    self = [super init];
    if (self != nil) 
    {       
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];

        self.locationManager.delegate = self;
    }
    return self;
}


- (void) timeoutHandler:(NSTimer *)_timer
{

    timer = nil;

    [self update_location];
}

-(void) update_location
{
    hasLocation=NO;

    [locationManager startUpdatingLocation];

    timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
                                         target: self
                                       selector: @selector(timeoutHandler:)
                                       userInfo: nil
                                        repeats: NO
         ];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"location ready");
    if(timer != nil) {
        [timer invalidate];
        timer = nil;
    }
    hasLocation = YES;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"location_ready"                         object:nil]; 


    if (debug_switch)
        NSLog(@" Delegate function, Getting new location from locationManager from Mylocation.m");


    _coordinate = newLocation.coordinate;

    source_lat=_coordinate.latitude;
    source_lng=_coordinate.longitude;

    //TRACE(@"new location: %f %f", _coordinate.latitude, _coordinate.longitude);
    //[delegate locationUpdate:newLocation.coordinate];

}
下面是一个演示如何使用它的教程:


好的,如果我不更改位置,会再次调用该回调吗?在位置更改之前不会调用该回调。有时,特别是在建筑物内部,但这是由于GPS信号本身的波动(因此回调将为您提供更改的位置)。而且,在调用startUpdatingLocation之后,即使您没有更改位置,也会立即调用回调。我认为在您的原始代码中,第二个和以后的startUpdatingLocation调用被忽略了。(这是我最好的猜测,我没有多次尝试给startUpdatingLocation打电话。)
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation