【问题标题】:MapKit Terminating app due to uncaught exception Invalid RegionMapKit 由于未捕获的异常无效区域而终止应用程序
【发布时间】:2012-10-14 21:43:21
【问题描述】:

我设置了mapView.delegate
一切都很好,但是当我离开时,在我恢复我的应用程序几分钟后,它就退出了。

控制台给了我这个,但我无法修复它:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'Invalid Region <center:-180.00000000, -180.00000000 span:+0.09000000, +0.09000000>'

userLocation 更新后在didUpdateUserLocation 方法中发送setRegion

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [mapView removeAnnotations: [mapView annotations]];
    for (TOJSearchItem *searchItem in _searchItems) {
        TOJPlaceRequest *request = [TOJPlaceRequest new];
        [request execute:SERVER_URL coordinate:userLocation.coordinate searchItem:searchItem delegate:self];
    }
    MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09f , 0.09f}};
    [mapView setRegion:region animated: YES];
}

提出请求

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (_lastLocation != nil) {
        CLLocationDistance meters = [newLocation distanceFromLocation:_lastLocation];
        if (meters < DISTANCE_MINI) {
//            CALCUL tout les points par rapport a current location
//            NSLog(@"%f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        } else {
            for (TOJSearchItem *searchItem in _searchItems) {
                TOJPlaceRequest *request = [TOJPlaceRequest new];
                [request execute:SERVER_URL coordinate:newLocation.coordinate searchItem:searchItem delegate:self];
            }
            MKCoordinateRegion region = {{newLocation.coordinate.latitude , newLocation.coordinate.longitude}, {0.09f , 0.09f}};
            [_mapView setRegion: region animated: YES];
            NSLog(@"reconnection");
        }
    }
    _lastLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
}

点击注释后重置区域

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if ([view class] == TOJClusterPushPinView.class) {
        TOJClusterPushPinView *clusterPushPinView = (TOJClusterPushPinView *)view;
        CLLocationCoordinate2D topLeftCoordinate;
        topLeftCoordinate.latitude = -90;
        topLeftCoordinate.longitude = 180;
        CLLocationCoordinate2D bottomRightCoordinate;
        bottomRightCoordinate.latitude = 90;
        bottomRightCoordinate.longitude = -180;
        for (TOJPushPin *pushPin in clusterPushPinView.clusterPushPin.pushPins) {
            topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, pushPin.coordinate.longitude);
            topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, pushPin.coordinate.latitude);
            bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, pushPin.coordinate.longitude);
            bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, pushPin.coordinate.latitude);
        }
        MKCoordinateRegion region;
        region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5;
        region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5;
        region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 2.0; // Add a little extra space on the sides
        region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 2.0; // Add a little extra space on the sides
        region = [mapView regionThatFits:region];
        [mapView setRegion:region animated:YES];
    }
}

【问题讨论】:

  • “当我离开时”究竟是什么意思(pop vc,dismiss vc,other)?显示调用 setRegion 的代码。
  • 当我切换应用程序(无论是什么应用程序)并返回此应用程序时,它会退出并在控制台上向我发送错误
  • 尝试应用this question 的建议。另请参阅 ng32rg 对该问题的回答。在您的情况下,最有用的检查可能是 if (!CLLocationCoordinate2DIsValid(center))

标签: iphone ios5 mapkit userlocation


【解决方案1】:

如果您的 didUpdateToLocation 方法,您应该检查给定的位置变量是否有效。

 if (userLocation.location.horizontalAccuracy >= 0) {

这是已知的行为,虽然我没有看到其中的逻辑,但它在恢复后获得的第一个位置可能是无效的。任何精度

参考:http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/instp/CLLocation/horizontalAccuracy

【讨论】:

  • 你有没有放断点或者日志语句看是didUpdateUserLocation还是didUpdateToLocation?我在您的回答中看到(您为什么要回答自己的问题来说您所显示的答案不起作用?)仅显示对 didUpdateToLocation 的更改。
  • 我在 didUpdateUserLocation 上放置了条件检查和断点,这是它在这条线上终止的问题:MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09 f , 0.09f}}; [mapView setRegion:region Animation: YES];
  • 您是否尝试过在 didUpdateUserLocation 中进行水平精度检查?
  • 是的,我做了,它也不起作用,我真的不明白为什么它不起作用
  • 看起来很奇怪,你得到的纬度和长度都是-180。您能否尝试打印出 lat/long 的值作为函数的第一行,然后在创建该区域之前再次打印。可能是您的 [request execute: search....] 正在修改值。
【解决方案2】:

改成

 if (userLocation.location.horizontalAccuracy >= 1) {

 .......

 }

有效

【讨论】:

    【解决方案3】:

    如果

    ,您应该检查 didUpdateUserLocation
    userLocation.location != nil
    

    在我的应用程序中,当从 applicationDidBecomeActive 事件返回时,第一个坐标始终无效。

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 2014-11-03
      • 2014-01-05
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多