开发者

How do I get CLLLocationManager to return immediately with a location?

I am trying to tweak my CLLocationManager 开发者_StackOverflow中文版settings so that when I call startUpdatingLocation it calls the delegate right away. Any idea on how to do this?


Thats not possible. The reason is simple, the device has no position fix that matches your desired accuracy the whole time. It might have to turn on the GPS chip to do this which also takes some time to ge a location (without additional infos and an outdated almanac this might take up to 15 minutes in the worst case).


It's not possible to tweak CLLocationManger in that way. However, what you could do is writing a wrapper around CLLocationManager which obtains the current position on startup and returns it everytime you ask for it. However, there might be situations where the location is not (yet) available, and this approach is quite power-consuming as you are using the GPS-device all the time.


This is by far NOT the best way (not even a good way probably) to use a CLLocationManager and when I find the time I am planning on going back and changing my code to only have a single CLLocationManager within my AppDelegate and have all sub view controller call the AppDelegate for their location needs.

I have the following code in a view controller (not the main controller or app delegate either), and about 5-10 seconds after the view controller is pushed by my main views navigation controller, it starts writing to NSLog with an accurate long/lat of my position (i.e. the delegate method locationManager:didUpdateToLocation:fromLocation: starts getting called):

- (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];
}

Then, in places like actionSheet:clickedButtonAtIndex:, I can get the location values on0demand with locationManager.location.coordinate.latitude / locationManager.location.coordinate.longitude or self.latitude / self.longitude.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜