【发布时间】:2023-12-22 08:30:01
【问题描述】:
@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 从未进入。为什么会有这样的差异?为什么没有输入回调?
【问题讨论】: