位置服务,经过一段时间(30秒)后如何更新纬度,经度?

时间:2011-07-30 12:26:10

标签: iphone objective-c ios nstimer cllocationmanager

我从位置服务获得纬度,经度,海拔高度,准确度,速度。

我想每30秒更新一次这些信息。为此,我使用了NSTimer:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES];

我已将viewDidLoad方法放入其中。它显示第一次输出,但第二次,当计时器调用它时,它显示此错误:

2011-08-02 13:17:00.141 CoreLocationAssign[1120:207] This is location   <__NSCFTimer: 0x4b200a0>
2011-08-02 13:17:00.142 CoreLocationAssign[1120:207] -[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0
2011-08-02 13:17:00.144 CoreLocationAssign[1120:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0'
*** Call stack at first throw:

代码如下:

MyCLController.h

@implementation MyCLController
@synthesize locationManager;
@synthesize delegate;

- (id) init{
    if (self!=nil) {
        self.locationManager  = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        [self.locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"Location: %@", [newLocation description]);
    [self.delegate locationUpdate:newLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
//  NSLog(@"Error: %@", [error description]);
    [self.delegate locationError:error];
}

- (void)dealloc {
    [self.locationManager release];
    [super dealloc];
}

CoreLoactionController.h
- (void)viewDidLoad {
    locationController = [[MyCLController alloc] init];
    locationController.delegate = self;
    [locationController.locationManager startUpdatingLocation];
    [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES];
    [super viewDidLoad];
}


- (void)locationUpdate:(CLLocation *)location {
    locationLable.text = [location description];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSLog(@"This is location   %@",[location description]);
    NSLog(@"Lattitude          =   %@",[NSString stringWithFormat:@"%f", coordinate.latitude]);
    NSLog(@"Langitude          =   %@",[NSString stringWithFormat:@"%f", coordinate.longitude]);
    NSLog(@"Altitude           =   %@",[NSString stringWithFormat:@"%gm", location.altitude]);
    NSLog(@"horizontalAccuracy =   %@",[NSString stringWithFormat:@"%gm", location.horizontalAccuracy]);
    NSLog(@"verticalAccuracy   =   %@",[NSString stringWithFormat:@"%gm", location.verticalAccuracy]);
    NSLog(@"Speed   =   %@",[NSString stringWithFormat:@"%gm", location.speed]);

}

如何解决这个问题呢?我想每隔30秒更新一次该位置。

3 个答案:

答案 0 :(得分:2)

有比使用计时器更好的解决方案。 CoreLocation内置了延迟更新的功能,与正常CoreLocation结合的计时器将破坏手机电池。尝试allowDeferredLocationUpdatesUntilDistanceTraveled:timeout:

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/allowDeferredLocationUpdatesUntilTraveled:timeout

答案 1 :(得分:0)

事实上这里:

- (void)locationUpdate:(CLLocation *)location

您没有收到CLLocation *,而是收到NSTimer。 您应该使用userInfo来传输对象:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:location repeats:YES];

-(void)locationUpdate:(NSTimer*) timer{
    //probably
    CLLocation *location = (CLLocation*)[timer userInfo];
    //...
}

答案 2 :(得分:0)

基于计时器的位置更新是可怕的电池食用者。

CoreLocation可以告诉您何时更改位置,无需轮询。如果我甚至走路,30秒的速度太慢了,如果我的手机躺在床边不动了好几个小时,就会不必要地摧毁电池。