tags:

views:

3301

answers:

2

I'm using a MKMapView inside an iPhone app. When I click a buttom the zoom level must increase. This is my first approach:

MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.5;
[mapView setRegion:zoomIn animated:YES];

However, this code had no effect, since I didn't update the longitudeDelta value. So I added this line:

zoomIn.span.longitudeDelta *= 0.5;

Now it works, but only sometimes. The latitudeDelta and longitudeDelta don't change in the same way, I mean, their values are not proportional. Any idea how to solve this?

+1  A: 

I use similar code to yours and it seems to work. What may be happening is that your delta is not changing enough to cause the zoom level to increase from one google zoom level to the next. This would also depend on the initial state of your map, which could explain why it is intermittent - so how do you set the map and zoom level up to begin with, before the user presses the zoom button?

You could also look into the regionThatFits method which will adjust the region you provide (name is from memory as I don't have the apple docs handy).

frankodwyer
+7  A: 

I have no idea if this is the right way to do it, but I'm using this to zoom in and out.

     case 0: { // Zoom In
  //NSLog(@"Zoom - IN");
  MKCoordinateRegion region;
  //Set Zoom level using Span
  MKCoordinateSpan span; 
  region.center=mapView.region.center;

  span.latitudeDelta=mapView.region.span.latitudeDelta /2.0002;
  span.longitudeDelta=mapView.region.span.longitudeDelta /2.0002;
  region.span=span;
  [mapView setRegion:region animated:TRUE];
 }
  break;

 // Zoom Out 
 case 2: {
  //NSLog(@"Zoom - OUT");
  MKCoordinateRegion region;
  //Set Zoom level using Span
  MKCoordinateSpan span; 
  region.center=mapView.region.center;
  span.latitudeDelta=mapView.region.span.latitudeDelta *2;
  span.longitudeDelta=mapView.region.span.longitudeDelta *2;
  region.span=span;
  [mapView setRegion:region animated:TRUE];
 }
dkardell