I am using MapKit, and every now and then I get the following error when starting the app.
'Invalid Region <center:nan, nan span:+180.00000000, +360.00000000>'
The code that I use for setting the region is the following.
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!self.initialLocation)
{
NSLog(@"Center is %f", userLocation.coordinate.longitude); // Debug
[self calculateLocationAddress]; // Reverse geolocating, does not create the error
self.initialLocation = userLocation.location;
MKCoordinateRegion region;
region.center = userLocation.coordinate;
// Region to show when app is loaded
region.span = MKCoordinateSpanMake(0.04, 0.04);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
}
The log in that method shows that the user location IS set, as I keep getting correct values in it, even though it crasches with the invalid region error. I have also tried using CLLocationCoordinate2DIsValid
to validate the region, same error though. As mentioned above, this error is very sporadical, showing up every now and then. Any ideas on what I could be doing wrong?
EDIT: The error pretty much always disappears if I open the storyboard and make a change to it before compiling again.
I have faced the same issue and it resolved from the below code.
if ( (region.center.latitude >= -90) && (region.center.latitude <= 90) && (region.center.longitude >= -180) && (region.center.longitude <= 180)) {
self.mapView.setRegion(region, animated: true)
}
else
{
print("Invalid region!")
}
Happy Coding Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.