无法设置 maprect 以显示所有注释

Failed to set maprect to show all annotations

我有 2 个注释要显示在 mapview 上,但无法将 maprect 设置为在不要求用户缩小的情况下在屏幕上显示所有注释。

我试过 showAnnotations 但没有成功。任何人都能够在 Swift 和 Xcode 6.1.1 中做到这一点?

这是我的代码:

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(k.x, k.y, 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.
    }


     }

这是我目前得到的:

这是我希望看到的:

感谢您的帮助。

终于找到为什么标注的引脚没有显示在屏幕可见区域了。我认为 MapKit 框架的行为与以前的版本有点不同。由于我使用自动布局允许地图扩展到所有设备(iPhone,iPad)的整个屏幕,地图的 setVisibleMapRect 或 mapView.showAnnotations 应该在 mapViewDidFinishLoadingMap 中调用,而不是在视图控制器的 viewDidLoad 中调用

例如:

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

我打电话时遇到了同样的问题

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

但是,将调用移动到 viewDidLayoutSubviews() 似乎也解决了问题(并不是 isInitialLoad 在 vi​​ewDidLoad 中初始化为 true)。

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

将调用放在viewDidLayoutSubviews 中的区别(我认为这是一个优点)是地图还没有实际显示,因此您的初始显示是注释定义的区域。但是,似乎每次地图缩放时都会调用它,因此您需要确保只在第一次调用它。

对我来说,在地图完成加载后显示注释无效。

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

除了显示注释外,我还需要计算多段线来连接注释,而且地图完成加载触发得太早了。

相反,我尝试了 mapViewDidFinishRenderingMap,它运行得非常好。请参阅下面的示例:

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

你可以试试这个。 我使用 swift 2.3 中的一些代码创建了一个扩展来显示所有注释。如果即使在最大缩放级别也无法显示,这将不会显示所有注释。

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)
    }
}