Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Can't tell where the invalid coordinates are coming from but I suggest adding the following checks to the <code>zoomToUserLocation</code> method.</p> <p>Just checking if <code>userlocation</code> is <code>nil</code> is not enough. You have to also check if the <code>location</code> property inside <code>userlocation</code> is nil or not. <em>Then</em>, you can use the <code>coordinate</code> property (especially when you're using the coordinates outside the <code>didUpdateUserLocation</code> delegate method). </p> <p>Also, just checking if <code>coordinate</code> is <code>0,0</code> (technically a valid coordinate) is not recommended as the struct will be "zero" if it's never been set or it could even be filled with random data. The Core Location framework's <code>CLLocationCoordinate2DIsValid</code> function is used as the last line of defense to prevent an invalid region.</p> <p>You could also check the <code>timestamp</code> and <code>horizontalAccuracy</code> if you want.</p> <pre><code>- (void)zoomToUserLocation:(MKUserLocation *)userlocation { if (!userlocation) return; if (!userlocation.location) { NSLog(@"actual location has not been obtained yet"); return; } //optional: check age and/or horizontalAccuracy //(technically should check if location.timestamp is nil first) NSTimeInterval locationAgeInSeconds = [[NSDate date] timeIntervalSinceDate:userlocation.location.timestamp]; if (locationAgeInSeconds &gt; 300) //adjust max age as needed { NSLog(@"location data is too old"); return; } if (!CLLocationCoordinate2DIsValid(userlocation.coordinate)) { NSLog(@"userlocation coordinate is invalid"); return; } MKCoordinateRegion region; region.center = userlocation.coordinate; region.span = MKCoordinateSpanMake(2.0, 2.0); //region = [self.mapView regionThatFits:region]; //don't need to call regionThatFits explicitly, setRegion will do it [self.mapView setRegion:region animated:YES]; } </code></pre> <p>Additionally (possibly unrelated and you may have already done this but), based on a couple of your previous questions related to this, you might want to clear and re-set the map view's <code>delegate</code> in the map view controller's <code>viewWillDisappear</code> and <code>viewWillAppear</code> methods to prevent certain errors:</p> <pre><code>-(void)viewWillAppear:(BOOL)animated { mapView.delegate = self; } -(void)viewWillDisappear:(BOOL)animated { mapView.delegate = nil; } </code></pre>
 

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