【问题标题】:Unrecognized Error in MapView (iOS)MapView (iOS) 中出现无法识别的错误
【发布时间】:2011-12-06 03:22:28
【问题描述】:

我在 MapView 中遇到错误,我无法识别并且找不到相关文档。它看起来像这样:

CoreAnimation: ignoring exception: 
Invalid Region <center:-180.00000000, -180.00000000 
                  span:+2.81462803, +28.12500000>

显然,这些数字现在是我的代码独有的,但我不知道发生了什么。 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];
}

【问题讨论】:

  • 中心坐标-180,-180无效。在某处,代码错误地设置了 region.center(尽管在这种情况下它通常会崩溃)。
  • region.center = userlocation.coordinate 在我用来缩放到用户位置的方法中。 'userlocation' 在方法中被设置为 MKUserLocation,所以它似乎没有意义......
  • 能否将方法代码添加到问题中?

标签: ios xcode core-animation android-mapview


【解决方案1】:

无法判断无效坐标的来源,但我建议在 zoomToUserLocation 方法中添加以下检查。

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

此外,不建议仅检查 coordinate 是否为 0,0(从技术上讲是一个有效坐标),因为如果从未设置过该结构,或者它甚至可以填充随机数据,则该结构将是“零”。 Core Location 框架的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];
}

另外(可能不相关,您可能已经这样做了,但是),基于您之前与此相关的几个问题,您可能希望在地图视图控制器的 @ 中清除并重新设置地图视图的 delegate 987654335@ 和viewWillAppear 方法来防止某些错误:

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

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

【讨论】:

  • 太好了,谢谢,这些都是防止 mapView 错误的好方法。尽我最大的努力学习所有这些。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2012-03-08
    • 2014-07-26
    相关资源
    最近更新 更多