MapKit 应用异常终止,由于未捕获的异常:无效区域。

3
我设置了mapView.delegate等内容。一切都很好,但当我离开应用几分钟后恢复它时,它会退出。
控制台给了我这个提示,但我还没有能够修复它:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'Invalid Region <center:-180.00000000, -180.00000000 span:+0.09000000, +0.09000000>'

在用户位置更新后,在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];
    }
}

“when I leave” 究竟是什么意思(弹出vc,解散vc,其他)?请展示调用setRegion的代码。 - user467105
当我切换应用程序(无论是哪个应用程序),然后返回到此应用程序时,它会退出并在控制台上发送错误。 - james075
尝试应用this question的建议。还可以查看ng32rg对该问题的回答。在您的情况下,最有用的检查可能是if (!CLLocationCoordinate2DIsValid(center)) - user467105
3个回答

0

在 didUpdateUserLocation 中,你应该进行检查

userLocation.location != nil

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

0

改为

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

 .......

 }

它可以工作


0

你是否设置了任何断点或日志语句来查看它是didUpdateUserLocation还是didUpdateToLocation?我在你的回答中看到(为什么要回答自己的问题,说你展示的答案不起作用?)只显示了对didUpdateToLocation的更改。 - Craig
我在 didUpdateUserLocation 上放置了条件检查和断点,问题出在这一行上:MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09f , 0.09f}}; [mapView setRegion:region animated: YES]; - james075
你在didUpdateUserLocation中尝试过horizontalAccuracy检查了吗? - Craig
是的,我试过了,它也没有起作用,我真的不明白为什么它不起作用。 - james075
看起来你得到的纬度和经度为-180非常奇怪。你可以尝试在函数的第一行打印出纬度/经度的值,然后再在创建区域之前再次打印出来。可能是因为你的[request execute: search....]正在修改这些值。 - Craig
启动应用程序:用户位置.coord - > 48.624554,2.467964 恢复应用程序:用户位置.coord - > -180.000000,-180.000000 在[请求...]中,我不修改值,只是向服务器发送注释请求。 lat = [[[venue objectForKey:@"location"] objectForKey: @"lat"] floatValue]; long = [[[venue objectForKey:@"location"] objectForKey: @"lng"] floatValue]; MKCoordinateRegion region = {{lat, long}, {0.009f, 0.009f}}; pu *pushPin = [[TOJPushPin alloc] initWithTitle:_searchItem.type coordinate:region.center address:address city:city]; [_searchItem.pushPins addObject:pushPin]; - james075

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接