2009-09-04 68 views
0

家伙,iPhone Corelocation框架限制?

我开发的,我需要的功能,要求用户输入其位置多次,就是发生在用户允许一次使用他的位置,当他转到另一部分它不要求一个应用程序他得到他的位置,它从已经缓存的位置取出。

是否有可能要求用户多次批准他的位置?

任何帮助表示赞赏。

干杯, 阿米特

回答

5

你并不需要获得许可多次。

首先获得更新,您拨打:

[locationManager startUpdatingLocation]; 

直到你调用[的LocationManager stopUpdatingLocation]你会得到持续的位置更新。

所以,你需要实现委托方法来说明当你得到一个新的位置时会发生什么。这个委托方法可以做一些简单的事情,比如将位置保存到一个类变量中供以后使用。

你需要实现的委托功能是:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation {} 

我应该在这里提醒你。持续运行GPS需要消耗大量电量(如果您要求尽可能快地读取非常准确的读数,您将在大约2.5小时内关闭设备)。所以,你应该做的是在用户打开应用程序时获得修复,然后调用stopUpdatingLocation。

然后,在您的应用程序中,有一个“定位我”按钮,它将打开LocationManager,获取修复程序,然后再次关闭LocationManager。你可能想继续查询一个位置,直到你获得一个好的位置。水平精度

我建议你实现一个NSObject子类,它实现LocationManagerDelegate协议。然后,这个对象将在多个视图控制器中共享。这是一个中央gpsController的简单实现。

所以,这将是gpsController.h:

#import <Foundation/Foundation.h> 
#import <CoreLocation/CoreLocation.h> 

@interface gpsController : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
    CLLocation *lastReading; 
} 


- (id)init; 

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, retain) CLLocation *lastReading; 

@end 

,然后将下面是gpsController.m:

#import "gpsController.h" 

@implementation gpsController 

@synthesize locationManager, lastReading; 

- (id)init { 

    if(self = [super init]) { 

    [[self locationManager] startUpdatingLocation]; 

    self.lastReading = nil; 

    } 

    return self; 

} 


- (CLLocationManager *)locationManager { 

    if (locationManager) return locationManager; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    locationManager.delegate = self; 

    return locationManager; 

} 


- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation { 

    self.lastReading = newLocation; 

} 
0

根据iPhone编程:大书呆子牧场指南,CLLocationManagers返回缓存,最后找到设备位置。如果didUpdateToLocation时间戳超过3分钟(假设它来自缓存),则它们的示例代码会从locationManager:didUpdateToLocation:fromLocation中退出。这似乎是有效更新之间相当长的时间,但也许只有通过做一些测试才能知道。 也许你可以坚持消息之间的时间戳,看看你是否仍然从缓存中获取位置数据,或者每次调用locationManager时增加一个计数器:didUpdateToLocation:fromLocation,并且只使用奇数编号的调用或其他增量。 。