【发布时间】:2014-04-11 13:12:29
【问题描述】:
我正在使用 MapKit,但在启动应用程序时不时出现以下错误。
'Invalid Region <center:nan, nan span:+180.00000000, +360.00000000>'
我用来设置区域的代码如下。
- (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];
}
}
该方法中的日志显示用户位置已设置,因为我不断在其中获得正确的值,即使它因无效区域错误而崩溃。我也尝试使用CLLocationCoordinate2DIsValid 来验证该区域,但同样的错误。如上所述,此错误非常零星,时不时出现。
关于我可能做错的任何想法?
编辑: 如果我打开情节提要并在再次编译之前对其进行更改,错误几乎总是会消失。
【问题讨论】:
-
我所知道的是问题是纬度必须从-90到+90,所以+180.00000000,+360.00000000超出范围。
-
进行测试运行显示我得到有效的经度和纬度,大约 +50 和 +10。
-
有时(从后台到前台,同时提示位置许可等),地图视图调用此委托方法时使用零位置或无效位置。在委托方法的顶部尝试这两件事。 1) 如果 userLocation.location 为 nil,退出方法(什么也不做)。 2) 如果 CLLocationCoordinate2DIsValid(userLocation.coordinate) 为 NO,则退出该方法(什么也不做)。
-
感谢@Anna 的提示,但即使在重新安装后运行应用程序时也会出现错误。你提到的两件事我也试过了。
-
为什么中心坐标是NaN?不是一个地区需要的吗?如果你想从 span 和 center 创建一个区域,那么你应该使用
MKCoordinateRegionMake(locationCentre, locationSpan)而locationCentre和locationSpan分别是 CLLocationCoordinate2D 和 MKCoordinateSpan。然后你在[mapView regionThatFits:theResultFromMake]中使用RegionMake的结果