Zoom in to match current location and map annotations

I am new to fast (programming on iOS in general) and trying to figure out how to enlarge the map so that it matches 2 points on the map.

I currently 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); 

That does nothing.

Do I need to somehow apply zoomRect to the map?

+8
swift mapkit
source share
7 answers

MKMapRectUnion computes and returns a new rectangle, nothing more. You must tell mapView to set your visible area to this new rectangle:

 myMapView.setVisibleMapRect(zoomRect, animated: true) 
+4
source share
 self.mapView.showAnnotations(self.mapView.annotations, animated: true) 
+14
source share

For quick 3, iOS 10

First find your current location and use the didUpdateLocations callback to configure the map. Add the following code to your view controller:

 @IBOutlet weak var mapView: MKMapView! private var locationManager: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() mapView.delegate = self locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest if CLLocationManager.locationServicesEnabled() { locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let last = newLocation { let userlongitude = last.coordinate.longitude let userlatitude = last.coordinate.latitude let newDistance = CLLocation(latitude: userlatitude, longitude: userlongitude).distance(from: CLLocation(latitude: YourAnnotation.coordinate.latitude, longitude: YourAnnotation.coordinate.longitude)) let region = MKCoordinateRegionMakeWithDistance(last.coordinate, 2 * newDistance, 2 * newDistance) let adjustRegion = self.mapView.regionThatFits(region) self.mapView.setRegion(adjustRegion, animated:true) } } 
+1
source share

-[MKMapView showAnnotations:animated:]

0
source share

As @lveselovsky said

 self.mapView.showAnnotations(self.mapView.annotations, animated: true) 

works great.

If you update your interface before loading the map, you can put it in the delegate method as follows:

 func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { guard !mapZoomUpdatedOnce else { return } self.mapView.showAnnotations(self.mapView.annotations, animated: true) self.mapZoomUpdatedOnce = true } 

Boolean value only to ensure that when it is updated for the first time, it will not be updated again if the user moves to another location on the map :)

0
source share

Swift 3 Set all annotations on the map. This is the right way.

 func zoomMapaFitAnnotations() { var zoomRect = MKMapRectNull for annotation in mapview.annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0) if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect } else { zoomRect = MKMapRectUnion(zoomRect, pointRect) } } self.mapview.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true) } 
0
source share

Use below MKMapView extension -

 extension MKMapView { func fitAllMarkers(shouldIncludeCurrentLocation: Bool) { if !shouldIncludeCurrentLocation { showAnnotations(annotations, animated: true) } else { var zoomRect = MKMapRectNull let point = MKMapPointForCoordinate(userLocation.coordinate) let pointRect = MKMapRectMake(point.x, point.y, 0, 0) zoomRect = MKMapRectUnion(zoomRect, pointRect) for annotation in annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0) if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect } else { zoomRect = MKMapRectUnion(zoomRect, pointRect) } } setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(8, 8, 8, 8), animated: true) } } } 
0
source share

All Articles