1

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?

HighFlyer
  • 1,615
  • 2
  • 15
  • 22

2 Answers2

1

I had the same problem, and it happen to be that I was sometimes changing the mapView size (for the needs of the application). Then I was using regionThatFits on my mapView when its height was 0.

As FKDev answer says, regionThatFits recalculates the span according to the mapView actual frame (what was causing the crash). Hope that can help someone.

aiwis31
  • 154
  • 2
  • 8
0

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)];
FKDev
  • 2,266
  • 1
  • 20
  • 23
  • Unfortunately, this does not helps. Problem is that code works well almost all time (span does not reduced), but sometimes such behavior happens. – HighFlyer Jul 12 '12 at 10:29
  • 1
    Well, in iOS 5.x and before, setRegion snaps to a grid: http://stackoverflow.com/questions/3612007/mkmapview-setregion-snaps-to-predefined-zoom-levels. Watch session 300 of WWDC 2012, around 5:00, you'll see there is hope for the future of setRegion. – FKDev Jul 29 '12 at 16:26