Reputation: 5413
@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];
}
The first time, I run the update_location routine, the location manager quickly jump to callback routine of didupdatetolocation. No Problem
However, next time I call the update_location function again, the callback didupdatetolocation never entered. Why such discrepancy? why the callback isn't entered?
Upvotes: 0
Views: 1026
Reputation: 4473
Your usage of LocationManager is incorrect. You only need to call startUpdatingLocation once, and the callback is invoked repeatedly whenever there is big enough change.
As of why with your observation, probably there was no significant change to invoke callback.
Upvotes: 2
Reputation: 39296
Why create a timer that keeps calling CLLocationManager startUpdatingLocation?
The CLLocationManager is typically used by setting yourself as the delegate. start it and then you get callbacks when the location changes.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
Here's a tutorial showing how to use it:
http://mobileorchard.com/hello-there-a-corelocation-tutorial/
Upvotes: 0