Suresh Varma
Suresh Varma

Reputation: 9740

Zoom issue in MKMapView when it is fully zoomed out

I am zooming out the map on the click of the button.

So when the map is fully zoomed out and if I try to again zoom out it then it crashes while setting the region.

Not sure but, Is there any way out to detect if the map has reached it maximum zoom limit?

Here is my code to zoom out the map

-(void)setZoomLevelForNoPicksCurrentLocationAt:(CLLocationCoordinate2D)currentCoordinate{
 MKCoordinateSpan span;
 MKCoordinateRegion region;
 region.center = currentCoordinate;
 span.latitudeDelta =self.mapView.region.span.latitudeDelta *2;
 span.longitudeDelta =self.mapView.region.span.longitudeDelta *2;
 region.span = span;
 [self.mapView setRegion:region animated:YES];  
 [self.mapView regionThatFits:region];
}

Any kind of help will be really appreciated.

Please find the crash log

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region center:+38.82259098, -67.50000000 span:+307.41536753, +450.00000000' * Call stack at first throw: ( 0 CoreFoundation 0x013c8be9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x0151d5c2 objc_exception_throw + 47 2 CoreFoundation 0x013c8b21 -[NSException raise] + 17 3 XXXapp 0x000ab237 -[MapViewController setZoomLevelForNoPicksCurrentLocationAt:] + 312

Upvotes: 3

Views: 3846

Answers (3)

Edouard Barbier
Edouard Barbier

Reputation: 1845

Solution in Swift 3.0 after a few trial and error tests.

Here is how I calculated my deltas:

let distanceLat = min(abs((currentLatitude! - pinLatitude) * 5), 180)
let distanceLon = min(abs((currentLongitude! - pinLongitude) * 5), 180)

The *5 is arbitrary, feel free to change it as you will.

Upvotes: 0

CTiPKA
CTiPKA

Reputation: 2974

I just added following checks to deltas calculation that prevent crashes on applying region:

//calculate deltas
double deltaLat = fabs(max_lat - min_lat) * 2;
if (deltaLat > 180) deltaLat = 180;

double deltaLong = fabs(max_long - min_long) * 2;
if (deltaLong>360) deltaLong = 360;

So the region delta latitude can not be more then 180 and longitude more then 360.

Upvotes: 4

iamsult
iamsult

Reputation: 1579

The minimum span value u can provide is 0.0 . At run-time the value of span is getting negative which causes the crash. Just hard cord and try and put a nslog to monitor the value of span. The maximum span value is 180.0 above that will result in crash.

Upvotes: 11

Related Questions