2009-09-04 70 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,並且只使用奇數編號的調用或其他增量。 。