Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

xcode - Unrecognized Error in MapView (iOS)

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 Region <center:-180.00000000, -180.00000000 
                  span:+2.81462803, +28.12500000>

Obviously the numbers are exclusive to my code right now but I can't figure out what's going on. The MapView runs just fine and all of my annotations show up (and it zooms on the user's location like I have it set). What specifically does this point to?

Thanks.


Here's the method I use to zoom to the user's location. It's a little unorthodox but it's what I've been helped with since I had problems with zooms for a variety of reasons (I can explain if need be, but it's probably not relevant):

- (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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...