Failed to set display to show all annotations

I have 2 annotations to display on the map, but it is not possible to set the display to show everything on the screen without requiring users to zoom out.

I tried with showAnnotations but no luck. Has anyone been able to do this in Swift and Xcode 6.1.1?

Here is my code:

 class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet var map: MKMapView! override func viewDidLoad() { super.viewDidLoad() var mapView = map // 1 let point1 = CLLocationCoordinate2D(latitude: 38.915565, longitude: -77.093524) let point2 = CLLocationCoordinate2D(latitude: 38.890693, longitude: -76.933318) //2 let annotation = MKPointAnnotation() annotation.setCoordinate(point1) annotation.title = "point1" map.addAnnotation(annotation) let annotation2 = MKPointAnnotation() annotation2.setCoordinate(point2) annotation2.title = "point2" map.addAnnotation(annotation2) //3 // option1: set maprect to cover all annotations, doesn't work var points = [annotation, annotation2] var rect = MKMapRectNull for p in points { let k = MKMapPointForCoordinate(p.coordinate) rect = MKMapRectUnion(rect, MKMapRectMake(kx, ky, 0.1, 0.1)) println("result: x = \(rect.origin.x) y = \(rect.origin.y)") } map.setVisibleMapRect(rect, animated: true) // option 2: using showAnnotations, doesn't work //map.showAnnotations(points, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

This is what I got so far: enter image description here

This is what I expected to see: enter image description here

Thank you for your help.

+7
swift mkmapview mkannotation mkmaprect
source share
4 answers

I finally found out why the annotation contacts did not appear in the visible area of ​​the screen. I think the MapKit structure behaves a little differently than in previous versions. Since I use autostart to expand the map to full screen for all devices (iPhone, iPad), setVisibleMapRect or mapView.show Map annotations should be called in mapViewDidFinishLoadingMap, and not in viewDidLoad of the view controller.

For example:

  func mapViewDidFinishLoadingMap(_ mapView: MKMapView) { // this is where visible maprect should be set mapView.showAnnotations(mapView.annotations, animated: true) } 
+18
source share

I had the same problem when I called

 viewDidLoad() { mapView.showAnnotations(myAnnotations, animated: false) } 

However, moving the viewDidLayoutSubviews () call also seems to fix the problem (not that isInitialLoad is initialized to true in viewDidLoad).

 viewDidLayoutSubviews() { if isInitialLoad { mapView.showAnnotations(myAnnotations, animated: false) isInitialLoad = false } } 

The difference (I think this is an advantage) is that the call to viewDidLayoutSubviews is that the map is not yet displayed, so your initial display is the area defined by annotations. However, it seems that it is called every time the map is scaled, so you must be sure that you only call it for the first time.

+3
source share

For me, using annotation impressions after the map finished loading didn't work.

 func mapViewDidFinishLoadingMap(mapView: MKMapView!) { // this is where visible maprect should be set mapView.showAnnotations(mapView.annotations, animated: true) } 

Besides showing the annotation, I needed to compute polylines for connecting annotations, and the map finished loading was called too soon.

Instead, I tried mapViewDidFinishRenderingMap and it worked fine. See the example below:

 //MARK: - Show all objects after adding them on the map func mapViewDidFinishRenderingMap(mapView: MKMapView, fullyRendered: Bool) { mapView.showAnnotations(mapStages, animated: true) } 
+2
source share

You can try this. I created an extension to show all annotations using some code here and there in swift 2.3. This does not display all annotations if they cannot be displayed even at the maximum zoom level.

 import MapKit extension MKMapView { func fitAllAnnotations() { var zoomRect = MKMapRectNull; for annotation in annotations { let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); zoomRect = MKMapRectUnion(zoomRect, pointRect); } setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(20, 20, 20, 20), animated: true) } } 
0
source share

All Articles