zhijie
zhijie

Reputation: 642

MKMapView NSInvalidArgumentException Invalid Region crash in ios6

Program crashes when set location coordinates using MKMapView. Log:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+112.57075000, +37.87049600 span:+0.05165163, +0.43945312>'

span in my program is

MKCoordinateSpan span;
span.latitudeDelta = .05;
span.longitudeDelta = .02;

after coding:

    self.mMKMapview.region = [self.mMKMapview regionThatFits:region];

as the log shows, span changes to :+0.05165163, +0.43945312

anyone help please, I have been standstill here for two days.

Thanks!

Upvotes: 14

Views: 9571

Answers (3)

KavonSakul
KavonSakul

Reputation: 49

I would rather suggest to use CLLocationCoordinate2DIsValid

so something like

guard CLLocationCoordinate2DIsValid(centerLat) else {

     return
}

Upvotes: 4

Roland Keesom
Roland Keesom

Reputation: 8298

I use the following code to set the region:

if( centerLat > -89 && centerLat < 89 && centerLng > -179 && centerLng < 179 ){
    [self.mapView setRegion:region animated:YES];
}

Upvotes: 3

user467105
user467105

Reputation:

The problem is the center coordinate:

+112.57075000, +37.87049600

The latitude must be from -90 to +90 so +112.57075 is out of range.

Check how the center coordinate is being set or maybe the data is backwards.


Also, you don't need to explicitly call regionThatFits because the map view does it automatically when you set the region normally (ie. just call setRegion). It's normal, by the way, for the map view to adjust the span as needed to fit the map view dimensions or zoom level.

Upvotes: 21

Related Questions