如何让CLLocationManager立即返回一个位置?

时间:2011-10-15 16:04:10

标签: iphone objective-c ipad cllocationmanager

我正在尝试调整我的CLLocationManager设置,以便在调用startUpdatingLocation时立即调用委托。关于如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:2)

多数民众赞成不可能。原因很简单,设备没有能够在整个时间内满足您所需精度的定位。它可能必须打开GPS芯片来执行此操作,这也需要一些时间来定位(没有额外的信息和过时的历书,在最坏的情况下这可能需要长达15分钟)。

答案 1 :(得分:0)

以这种方式调整CLLocationManger是不可能的。但是,你可以做的是在CLLocationManager周围编写一个包装器,它在启动时获取当前位置,并在每次请求时返回它。但是,可能存在位置尚未(现在)可用的情况,并且这种方法非常耗电,因为您始终使用GPS设备。

答案 2 :(得分:0)

到目前为止,这不是最佳方式(可能不是很好的方式)使用CLLocationManager,当我找到时间计划返回并更改我的代码时在我的AppDelegate中只有一个CLLocationManager并让所有子视图控制器调用AppDelegate以满足其位置需求。

我在视图控制器中有以下代码(不是主控制器或app委托),在我的主视图导航控制器推动视图控制器后约5-10秒,它开始写入{{1}我的位置有一个准确的long / lat(即委托方法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: / locationManager.location.coordinate.latitudelocationManager.location.coordinate.longitude / {{1获取位置值 on0demand }}