Непоследовательное местоположение Менеджер сделал обновление местоположения на Iphone - PullRequest
0 голосов
/ 29 декабря 2011
@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];

}

В первый раз, когда я запускаю процедуру update_location, менеджер местоположения быстро переходит к процедуре обратного вызова didupdatetolocation. Нет проблем

Однако при следующем вызове функции update_location обратный вызов didupdatetolocation никогда не входил. Почему такое несоответствие? почему обратный вызов не введен?

Ответы [ 2 ]

2 голосов
/ 29 декабря 2011

Ваше использование LocationManager неверно.Вам нужно вызывать startUpdatingLocation только один раз, и обратный вызов вызывается повторно всякий раз, когда происходит достаточно большое изменение.

По состоянию на ваш взгляд, вероятно, не было никаких существенных изменений для вызова обратного вызова.

0 голосов
/ 29 декабря 2011

Зачем создавать таймер, который продолжает вызывать CLLocationManager startUpdatingLocation?

CLLocationManager обычно используется, устанавливая себя в качестве делегата.запустите его, а затем вы получите обратные вызовы при изменении местоположения.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Вот учебник, показывающий, как его использовать:

http://mobileorchard.com/hello-there-a-corelocation-tutorial/

...