2011-10-15 38 views

回答

2

那是不可能的。原因很簡單,該設備沒有定位,可以隨時匹配您所需的精度。它可能不得不打開GPS芯片來做到這一點,這也需要一些時間來創建一個位置(沒有額外的信息和過時的年曆,在最壞的情況下這可能需要長達15分鐘)。

0

以這種方式調整CLLocationManger是不可能的。然而,你可以做的是圍繞CLLocationManager編寫一個包裝器,它在啓動時獲得當前位置,並在你每次請求時返回它。但是,可能存在位置尚未(可用)的情況,並且隨着您始終使用GPS設備,此方法非常耗電。

0

這是到目前爲止還沒有在最佳方式(甚至不是一個很好的大概樣子)使用CLLocationManager,當我發現我上回去和改變我的代碼只有一個CLLocationManager內規劃的時候我AppDelegate並讓所有的子視圖控制器調用AppDelegate以獲取其位置需求。

我在視圖控制器(不是主控制器或應用程序委託)中有以下代碼,並且在視圖控制器被我的主視圖導航控制器按下後約5-10秒,它開始用NSLog精準的長/緯度我的位置(即委託locationManager:didUpdateToLocation:fromLocation:開始被稱爲法):

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // .... 
    self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager startUpdatingLocation]; 
    // .... 
} 

// This will be called just a few seconds after the view appears and the 
// location is accurate (if I have location services on and good sky-line of sight) 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSDate* eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (abs(howRecent) < 5.0) { 
     self.latitude = [NSNumber numberWithDouble:newLocation.coordinate.latitude]; 
     self.longitude = [NSNumber numberWithDouble:newLocation.coordinate.longitude]; 
     NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 
    } 
} 

// Then, tear down the location Manager (so I can use it in another view controller) 
// A bit excessive possibly, but I like to be absolutely sure that my App does not use the GPS 
// when it doesn't need it to save batter life. 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [locationManager stopMonitoringSignificantLocationChanges]; 
    [locationManager stopUpdatingHeading]; 
    [locationManager stopUpdatingLocation]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
    [locationManager stopMonitoringSignificantLocationChanges]; 
    [locationManager stopUpdatingHeading]; 
    [locationManager stopUpdatingLocation]; 
} 

- (void)viewDidUnload { 
    [locationManager stopUpdatingLocation]; 
    [locationManager stopMonitoringSignificantLocationChanges]; 
    [locationManager stopUpdatingHeading]; 
    [locationManager setDelegate:nil]; 
    self.locationManager = nil; 
} 

- (void)dealloc { 
    // .... 
    [locationManager stopUpdatingLocation]; 
    [locationManager setDelegate:nil]; 
    [locationManager release], locationManager = nil; 
    // .... 
    [super dealloc]; 
} 

然後,像actionSheet:clickedButtonAtIndex:的地方,我能得到的位置值on0demandlocationManager.location.coordinate.latitude/locationManager.location.coordinate.longitudeself.latitude/self.longitude