Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom to fit current location and annotation on map

Tags:

swift

mapkit

I am new to swift (iOS programming in general) and am trying to figure out how to zoom a map out to fit 2 points on the map.

Currently I have

var zoomRect = MKMapRectNull;
var myLocationPointRect = MKMapRectMake(myLocation.longitude, myLocation.latitude, 0, 0)
var currentDestinationPointRect = MKMapRectMake(currentDestination.longitude, currentDestination.latitude, 0, 0)

zoomRect = myLocationPointRect;
zoomRect = MKMapRectUnion(zoomRect, currentDestinationPointRect);

Which does nothing.

Do I have to apply zoomRect to the map somehow?

like image 875
Wesley Skeen Avatar asked Nov 26 '14 19:11

Wesley Skeen


Video Answer


2 Answers

self.mapView.showAnnotations(self.mapView.annotations, animated: true)
like image 167
lveselovsky Avatar answered Sep 27 '22 17:09

lveselovsky


MKMapRectUnion computes and returns a new rect, nothing more. You need to tell the mapView to set its visible area to that new rect:

myMapView.setVisibleMapRect(zoomRect, animated: true)
like image 32
zisoft Avatar answered Sep 27 '22 16:09

zisoft