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

标签 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 秒更新一次位置。

最佳答案

有比使用计时器更好的解决方案。 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 :

关于iphone - 位置服务,如何在一段时间(30 秒)后更新纬度、经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6883089/

相关文章:

ios - 在 iOS 上导航时 Xamarin Forms NavigationPage 上一页的一小部分可见

ios - 了解代码是否是针对 iPhone、iPad 或 Universal 编译的

iphone - UIAlertView 中 UIView 中的 UITextField 不起作用?

php - “无法设置私钥文件”试图使用 php 发送推送

ios - 保存 PFObject NSCoding

ios - 使用不同的行数重新加载 UITableView

iOS - cellForRowAtIndexPath 代码解释

ios - 杀死应用程序后如何在后台执行某些任务

iphone - 在Objective-C中,与类方法相比,拥有单例有什么优势?

iphone - 为什么自定义 TableViewCell 不需要文件所有者?