@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从未进入。为什么会出现这种差异?为什么没有输入回调?
答案 0 :(得分:2)
您对LocationManager的使用不正确。您只需要调用一次startUpdatingLocation,并且只要有足够大的变化就会重复调用回调。
至于为什么你的观察,可能没有重大的变化来调用回调。
答案 1 :(得分:0)
为什么要创建一个不断调用CLLocationManager startUpdatingLocation的计时器?
CLLocationManager通常用于将自己设置为委托。启动它,然后在位置发生变化时获得回调。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
这是一个展示如何使用它的教程:
http://mobileorchard.com/hello-there-a-corelocation-tutorial/