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: 
This is what I expected to see: 
Thank you for your help.
swift mkmapview mkannotation mkmaprect
tala9999
source share