我想调整我的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发送一个异步任务,并立即返回