Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom in a MKMapView programmatically

I'm using a MKMapView inside an iPhone app. When I click a button 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?

like image 201
Hectoret Avatar asked Jun 23 '09 10:06

Hectoret


People also ask

How do I zoom in on mapView?

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

How do you zoom map in react native?

It takes a region object which has latitudeDelta and longitudeDelta . Use these to set the zoom level. Updated: in a Region object the latitude and longitude specify the center location and latitudeDelta and longitudeDelta specify the span of the viewable map area.


2 Answers

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];     } 
like image 163
dkardell Avatar answered Oct 19 '22 03:10

dkardell


Just cleaning up dkdarel's answer

// delta is the zoom factor // 2 will zoom out x2 // .5 will zoom in by x2 - (void)zoomMap:(MKMapView*)mapView byDelta:(float) delta {      MKCoordinateRegion region = mapView.region;     MKCoordinateSpan span = mapView.region.span;     span.latitudeDelta*=delta;     span.longitudeDelta*=delta;     region.span=span;     [mapView setRegion:region animated:YES];  } 

Swift Code:

func zoomMap(byFactor delta: Double) {     var region: MKCoordinateRegion = self.mapView.region     var span: MKCoordinateSpan = mapView.region.span     span.latitudeDelta *= delta     span.longitudeDelta *= delta     region.span = span     mapView.setRegion(region, animated: true) } 
like image 30
Kevin Avatar answered Oct 19 '22 01:10

Kevin