MapView(iOS)中无法识别的错误

我在MapView中收到一个我无法识别但无法找到文档的错误。 它看起来像这样:

CoreAnimation: ignoring exception: Invalid Region  

显然,这些数字现在仅限于我的代码,但我无法弄清楚发生了什么。 MapView运行得很好,我的所有注释都会显示出来(它会像我设置的那样放大用户的位置)。 具体到底是什么意思?

谢谢。


这是我用来缩放到用户位置的方法。 这有点不正统,但这是我得到帮助的原因,因为我出于各种原因遇到缩放问题(我可以解释一下,如果需要,但它可能不相关):

 - (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]; } 

无法分辨无效坐标的来源,但我建议将以下检查添加到zoomToUserLocation方法。

只检查userlocation是否为nil是不够的。 您还必须检查userlocation内的location属性userlocation为nil。 然后 ,您可以使用coordinate属性(特别是当您使用didUpdateUserLocation委托方法之外的坐标时)。

此外,不建议仅检查coordinate是否为0,0 (技术上是有效坐标),因为如果结构从未设置过,结构将为“零”,或者甚至可以用随机数据填充结构。 核心位置框架的CLLocationCoordinate2DIsValid函数用作防止无效区域的最后一道防线。

如果需要,您还可以检查timestamphorizontalAccuracy

 - (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 > 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]; } 

另外(可能没有关联,你可能已经这样做但是),基于你之前的几个与之相关的问题,你可能想要在地图视图控制器的viewWillDisappearviewWillAppear方法中清除并重新设置地图视图的delegate以防止某些错误:

 -(void)viewWillAppear:(BOOL)animated { mapView.delegate = self; } -(void)viewWillDisappear:(BOOL)animated { mapView.delegate = nil; } 

我发现如果启用了位置服务,然后显示包含当前用户位置作为注释的地图视图,则禁用位置服务并尝试使用注释的“location”属性,结果将为(-180) ,-180)。

Interesting Posts