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

Mil*_*dia 3 xcode core-animation ios android-mapview

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

CoreAnimation: ignoring exception: 
Invalid Region <center:-180.00000000, -180.00000000 
                  span:+2.81462803, +28.12500000>
Run Code Online (Sandbox Code Playgroud)

显然这些数字现在是我的代码所独有的,但我无法弄清楚发生了什么.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];
}
Run Code Online (Sandbox Code Playgroud)

小智 6

无法分辨无效坐标的来源,但我建议在zoomToUserLocation方法中添加以下检查.

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

此外,刚刚检查,如果coordinate0,0(在技术上有效坐标),不推荐,因为该结构将是"零",如果它没有被设置,或者甚至可以填充随机数据.Core Location框架的CLLocationCoordinate2DIsValid功能用作防止无效区域的最后一道防线.

你也可以检查一下timestamp,horizontalAccuracy如果你想.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

-(void)viewWillDisappear:(BOOL)animated
{
    mapView.delegate = nil;
}
Run Code Online (Sandbox Code Playgroud)