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];
}
I have found that if you have location services enabled and then display a map view that contains the current user location as an annotation, then disable location services and attempt to use the "location" property of the annotation, the result will be (-180, -180).
Can't tell where the invalid coordinates are coming from but I suggest adding the following checks to the
zoomToUserLocation
method.Just checking if
userlocation
isnil
is not enough. You have to also check if thelocation
property insideuserlocation
is nil or not. Then, you can use thecoordinate
property (especially when you're using the coordinates outside thedidUpdateUserLocation
delegate method).Also, just checking if
coordinate
is0,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'sCLLocationCoordinate2DIsValid
function is used as the last line of defense to prevent an invalid region.You could also check the
timestamp
andhorizontalAccuracy
if 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
delegate
in the map view controller'sviewWillDisappear
andviewWillAppear
methods to prevent certain errors: