Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've had lots of issues with CLLocationManager on the iPhone also. From different Apps, and trial and error this is what I've figured out;</p> <p>1) Use a singleton class for the locationManager, ONLY ONE Instance, follow the examples in:</p> <p>Apple's LocateMe example</p> <p>Tweetero : <a href="http://tweetero.googlecode.com/svn/trunk" rel="nofollow noreferrer">http://tweetero.googlecode.com/svn/trunk</a></p> <p>I'm thinking you can only have one location manager running, there's only one GPS chip in the iPhone, thus if you launch multiple location manager object's they'll conflict, and you get errors from the GPS location manager stating it can't update.</p> <p>2) Be extremely careful how you update the <code>locationManager.desiredAccuracy</code> and <code>locationManager.distanceFilter</code> </p> <p>Follow the example in Apple's code for this in the FlipsideViewController:</p> <pre><code>[MyCLController sharedInstance].locationManager.desiredAccuracy = accuracyToSet; [MyCLController sharedInstance].locationManager.distanceFilter = filterToSet; </code></pre> <p>This approach works, and causes no errors. If you update the desiredAccuracy or distanceFilter in the main delegate loop:</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation </code></pre> <p>Core Location will likely complain that it can't update the values, and give you errors. Also, only use the constants for the <code>desiredAccuracy</code> supplied by Apple, and no others, thus; </p> <blockquote> <p>kCLLocationAccuracyBest, kCLLocationAccuracyNearestTenMeters,<br> kCLLocationAccuracyHundredMeters, kCLLocationAccuracyKilometer,<br> kCLLocationAccuracyThreeKilometers</p> </blockquote> <p>Use of other values might give you errors from locationManager. For <code>distanceFilter</code> you can use any value, but be aware people have stated it has issues, the main value to default to is: -1 = Best, I've used 10.0 and it seems OK.</p> <p>3) To force a new update, call the <code>startUpdates</code> method like in Tweetero, this forces locationManager to do it's thing, and it should try and get you a new value.</p> <p>4) The core location delegate routine is tricky to get accurate updates out of. When your App first initializes you can be off in accuracy by 1000 meters or more, thus one attempt is not going to do it. Three attempts after initialization usually gets you to within 47 or so meters which is good. Remember that each successive attempt is going to take longer and longer to improve your accuracy, thus it gets expensive quickly. This is what you need to do: Take a new value only if it is recent AND If the new location has slightly better accuracy take the new location OR If the new location has an accuracy &lt; 50 meters take the new location OR If the number of attempts has exceeded 3 or 5 AND the accuracy &lt; 150 meters AND the location has CHANGED, then this is a new location and the device moved so take this value even if it's accuracy is worse. Here's my Location code to do this:</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  *)newLocation  fromLocation:(CLLocation *)oldLocation { locationDenied = NO; if(![[NSUserDefaults standardUserDefaults] boolForKey:@"UseLocations"]) { [self stopAllUpdates]; return; } NSDate* eventDate = newLocation.timestamp; NSTimeInterval howRecent = abs([eventDate timeIntervalSinceNow]); attempts++; if((newLocation.coordinate.latitude != oldLocation.coordinate.latitude) &amp;&amp; (newLocation.coordinate.longitude != oldLocation.coordinate.longitude)) locationChanged = YES; else locationChanged = NO; #ifdef __i386__ //Do this for the simulator since location always returns Cupertino if (howRecent &lt; 5.0) #else // Here's the theory of operation // If the value is recent AND // If the new location has slightly better accuracy take the new location OR // If the new location has an accuracy &lt; 50 meters take the new location OR // If the attempts is maxed (3 -5) AND the accuracy &lt; 150 AND the location has changed, then this must be a new location and the device moved // so take this new value even though it's accuracy might be worse if ((howRecent &lt; 5.0) &amp;&amp; ( (newLocation.horizontalAccuracy &lt; (oldLocation.horizontalAccuracy - 10.0)) || (newLocation.horizontalAccuracy &lt; 50.0) || ((attempts &gt;= attempt) &amp;&amp; (newLocation.horizontalAccuracy &lt;= 150.0) &amp;&amp; locationChanged))) #endif { attempts = 0; latitude = newLocation.coordinate.latitude; longitude = newLocation.coordinate.longitude; [currentLocation release]; currentLocation = [newLocation retain]; goodLocation = YES; [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateLocationNotification" object: nil]; if (oldLocation != nil) { distanceMoved = [newLocation getDistanceFrom:oldLocation]; currentSpeed = newLocation.speed; distanceTimeDelta = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp]; } } // Send the update to our delegate [self.delegate newLocationUpdate:currentLocation]; } </code></pre> <p>If you have a iPhone 3GS getting Heading updates is a lot easier, here's the code in the same module as above:</p> <pre><code>//This is the Heading or Compass values - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { if (newHeading.headingAccuracy &gt; 0) { [newHeading retain]; // headingType = YES for True North or NO for Magnetic North if(headingType) { currentHeading = newHeading.trueHeading; } else { currentHeading = newHeading.magneticHeading; } goodHeading = YES; [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateHeadingNotification" object: nil]; } } </code></pre> <p>Hope this helps people!</p>
 

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