Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible for CLLocationManager to be affected by other apps?
    primarykey
    data
    text
    <p>I am running into a very strange situation with an app I have that uses CLLocationManager to get the user's current location. I am using a wrapper class that is very similar to Apple's CLLocationManager example code. It starts looking for location, and it waits until it is given a location that meets some criteria (timestamp age, accuracy). All works well when I am in a very GPS-accessible area. </p> <p>Now the issue. When I am in my office, on a WiFi connection, where the GPS signal is seemingly lousy, I open my app and it can never find a suitable location. I exit out of my app, open Foursquare, and it finds places near me almost immediately, leading me to assume that it has found my location. I exit Foursquare, and reopen my app to find that it finds my location almost instantly. </p> <p>Can anyone shed some light on what might be happening here? I can post some code if folks think that would be helpful, but this is more a general question about how other apps can positively or negatively affect the functionality of CLLocationManager. Ideally, I'd love to know what exactly Foursquare is doing to get a location so quickly, and how that might cause my app to suddenly start getting one as well. </p> <p>EDIT: It seems like based on the answer below, people have experienced cross-app cacheing, which is fine. However, it doesn't explain why Foursquare gets a location and my app only gets it after using Foursquare. Below is my CLLocationManager code, hopefully someone can find some smoking gun:</p> <pre><code>- (void) lazyInit { if (!self.locationManager) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; self.reverseGeocoder = [[WPGeocodingService alloc] initWithDelegate:self]; self.locationAndPlacemark = NO; } } - (void) getCurrentLocation { [self lazyInit]; self.recentLocation = nil; [self performSelector:@selector(timeoutLocationFetch) withObject:nil afterDelay:kLocationFetchTimeout]; [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"LocationService:update: &lt;%f,%f&gt; Accuracy: %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.horizontalAccuracy); // test the age of the location measurement to determine if the measurement is cached // in most cases you will not want to rely on cached measurements NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge &gt; 5.0) return; // test that the horizontal accuracy does not indicate an invalid measurement if (newLocation.horizontalAccuracy &lt; 0) return; // test the measurement to see if it is more accurate than the previous measurement if (self.recentLocation == nil || self.recentLocation.horizontalAccuracy &gt; newLocation.horizontalAccuracy) { // store the location as the "best effort" self.recentLocation = newLocation; // test the measurement to see if it meets the desired accuracy // // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout. // if (newLocation.horizontalAccuracy &lt;= self.locationManager.desiredAccuracy) { // we have a measurement that meets our requirements, so we can stop updating the location // // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible. // [self.locationManager stopUpdatingLocation]; // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeoutLocationFetch) object:nil]; if ([self.delegate respondsToSelector:@selector(locationService:didReceiveLocation:)]) { [self.delegate locationService:self didReceiveLocation:self.recentLocation]; } } } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"LocationService:error: %@", [error description]); if ([error code] != kCLErrorLocationUnknown) { [self.locationManager stopUpdatingLocation]; if ([self.delegate respondsToSelector:@selector(locationService:didFailWithError:)]) { [self.delegate locationService:self didFailWithError:error]; } } } - (void) timeoutLocationFetch { NSLog(@"LocationService:timeout"); [self.locationManager stopUpdatingLocation]; if (self.recentLocation &amp;&amp; [self.delegate respondsToSelector:@selector(locationService:didReceiveLocation:)]) { if ([self.delegate respondsToSelector:@selector(locationService:didReceiveLocation:)]) { [self.delegate locationService:self didReceiveLocation:self.recentLocation]; } } else { NSError* error = [NSError errorWithDomain:@"Location Error" code:0 userInfo: [NSDictionary dictionaryWithObject:@"The application could not determine your location." forKey:NSLocalizedDescriptionKey]]; if ([self.delegate respondsToSelector:@selector(locationService:didFailWithError:)]) { [self.delegate locationService:self didFailWithError:error]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload