我想調整我的CLLocationManager設置,以便在我調用startUpdatingLocation時立即調用委託。任何想法如何做到這一點?如何讓CLLLocationManager立即返回一個位置?
0
A
回答
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:
的地方,我能得到的位置值on0demand與locationManager.location.coordinate.latitude
/locationManager.location.coordinate.longitude
或self.latitude
/self.longitude
。
相關問題
- 1. 如何讓ssh立即返回
- 2. 如何立即返回
- 3. 第一個Console.ReadLine()立即返回
- 4. Check_output立即返回
- 5. getchar()立即返回
- 6. DispatcherOperation.Task立即返回
- 7. 如何讓Java rest api調用立即不返回等待?
- 8. 如何讓元素在CSS動畫結束後立即回到初始位置?
- 9. Android:Selector.select()立即返回0
- 10. boost :: condition_variable.timed_wait立即返回
- 11. WPF ShowDialog立即返回
- 12. paypal立即購買返回
- 13. System.Net.Http.Handlers.ProgressMessageHandler立即返回100%
- 14. Settimeout不立即返回
- 15. pthread_cond_timedwait立即返回ETIMEDOUT
- 16. gcloud-java pubsub API:如何設置「立即返回」標誌
- 17. FD_READ:recv()立即返回兩個數據包
- 18. 立即在排序位置中放置一個值
- 19. 如何返回createdatroute位置?
- 20. 如何讓PHP在特定位置的表單中返回一個錯誤
- 21. 如何立即回覆?
- 22. 如何位移位立即值
- 23. 如何讓返回按鈕返回到UITableView中的相同位置
- 24. 班級返回一箇舊位置
- 25. 返回一個元素的位置Clojure
- 26. 當頁面被調用時立即返回一個Json對象
- 27. 在調用另一個函數後立即返回函數
- 28. 在另一個方法中調用ServiceController.WaitForStatus立即返回
- 29. RxJava:同步/立即返回一個緩存的值
- 30. 使用GPars發送一個異步任務,並立即返回