如何防止 MapKit 在打开时最初缩放?

How to prevent MapKit from initially zooming when it is opened?

我有一些位置的地图视图,这些位置是我从之前的 segue 的准备功能中获取的。我在视图中有一个 for in 循环确实加载,以便我可以进行注释并显示它们。这一切工作正常,但是当我单击前一个视图控制器中的显示地图按钮时,它会将我带到我的地图视图,并且 mapView 会自动放大很多到第一个注释上。这可能是个问题,因为我希望用户看到所有位置。

我的问题是,如何阻止地图的初始缩放?

下面是我的 viewDidLoad 代码。

顺便说一句,bins 是我在 core data 中的实体,mapView 是我连接到 storyboard 中的 Map Kit 的 IBOutlet 变量。

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        for bin in bins {
        let geoCoder = CLGeocoder()
        geoCoder.geocodeAddressString(bin.address ?? "", completionHandler: { placemarks, error in
            if let error = error {
                print(error)
                return
            }

    
            if let placemarks = placemarks {
                // Get the first placemark
                let placemark = placemarks[0]

                let annotation = MKPointAnnotation()
                annotation.title = bin.type

                if let location = placemark.location {
                    annotation.coordinate = location.coordinate
                    // Display the annotation
                    self.mapView.showAnnotations([annotation], animated: true)
                    self.mapView.selectAnnotation(annotation, animated: true)
                
                }
            }
            
        }
    )}
}`

这一行: self.mapView.showAnnotations([annotation], animated: true) 缩放地图以显示单个注释,而不是所有注释。您需要创建一个包含所有注释的数组,并在创建所有注释后显示。