I'm working on component using MKMapView. Map should double zoom on annotation tap. To do this, I try to reduce twice map region span, but sometimes it works incorrectly: Here peace of code:
MKCoordinateSpan newSpan = mapView.region.span;
NSLog(@"old: %f, %f", newSpan.latitudeDelta, newSpan.longitudeDelta);
newSpan = MKCoordinateSpanMake(newSpan.latitudeDelta / 2.0, newSpan.longitudeDelta / 2.0);
NSLog(@"new: %f, %f", newSpan.latitudeDelta, newSpan.longitudeDelta);
MKCoordinateRegion region = [mapView regionThatFits:MKCoordinateRegionMake(centerCoordinate, newSpan)];
NSLog(@"!!!! (%f, %f) (%f, %f)", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta);
I take current span, reduce it and pass to regionThatFits. Sometimes results are:
old: 0.609257, 0.914612
new: 0.304629, 0.457306
!!!! (55.805472, 37.579371) (0.608178, 0.914612)
regionThatFits doubles span passed to it. So visual effect is centering of view annotation without zooming.
Any suggestions?
You shouldn't use regionThatFits:
because it is used to change the span value to match the view frame.
From Apple's Doc :
A region that is still centered on the same point of the map but whose span values are adjusted to fit in the map view’s frame.
In your case, you can just change the span value of the map region directly.
[mapView setRegion:MKCoordinateRegionMake(centerCoordinate, newSpan)];