Unrecognized Error in MapView (iOS)

前端 未结 2 512
南旧
南旧 2020-12-10 08:59

I\'m getting an error in MapView that I don\'t recognize and can\'t find documentation on. It looks like this:

CoreAnimation: ignoring exception: 
Invalid Re         


        
2条回答
  •  春和景丽
    2020-12-10 09:36

    Can't tell where the invalid coordinates are coming from but I suggest adding the following checks to the zoomToUserLocation method.

    Just checking if userlocation is nil is not enough. You have to also check if the location property inside userlocation is nil or not. Then, you can use the coordinate property (especially when you're using the coordinates outside the didUpdateUserLocation delegate method).

    Also, just checking if coordinate is 0,0 (technically a valid coordinate) is not recommended as the struct will be "zero" if it's never been set or it could even be filled with random data. The Core Location framework's CLLocationCoordinate2DIsValid function is used as the last line of defense to prevent an invalid region.

    You could also check the timestamp and horizontalAccuracy if you want.

    - (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];
    }
    

    Additionally (possibly unrelated and you may have already done this but), based on a couple of your previous questions related to this, you might want to clear and re-set the map view's delegate in the map view controller's viewWillDisappear and viewWillAppear methods to prevent certain errors:

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

提交回复
热议问题