I’m getting an error in MapView that I don’t recognize and can’t find documentation on. It looks like this:
CoreAnimation: ignoring exception:
Invalid Region <center:-180.00000000, -180.00000000
span:+2.81462803, +28.12500000>
Obviously the numbers are exclusive to my code right now but I can’t figure out what’s going on. The MapView runs just fine and all of my annotations show up (and it zooms on the user’s location like I have it set). What specifically does this point to?
Thanks.
Here’s the method I use to zoom to the user’s location. It’s a little unorthodox but it’s what I’ve been helped with since I had problems with zooms for a variety of reasons (I can explain if need be, but it’s probably not relevant):
- (void)zoomToUserLocation:(MKUserLocation *)userlocation
{
if (!userlocation)
return;
MKCoordinateRegion region;
region.center = userlocation.coordinate;
region.span = MKCoordinateSpanMake(2.0, 2.0);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self zoomToUserLocation:self.mapView.userLocation];
}
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)location
{
[self zoomToUserLocation:location];
}
Can’t tell where the invalid coordinates are coming from but I suggest adding the following checks to the
zoomToUserLocationmethod.Just checking if
userlocationisnilis not enough. You have to also check if thelocationproperty insideuserlocationis nil or not. Then, you can use thecoordinateproperty (especially when you’re using the coordinates outside thedidUpdateUserLocationdelegate method).Also, just checking if
coordinateis0,0(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’sCLLocationCoordinate2DIsValidfunction is used as the last line of defense to prevent an invalid region.You could also check the
timestampandhorizontalAccuracyif you want.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
delegatein the map view controller’sviewWillDisappearandviewWillAppearmethods to prevent certain errors: