【问题标题】:How to prevent MapKit from initially zooming when it is opened?如何防止 MapKit 在打开时初始缩放?
【发布时间】:2020-07-10 14:50:06
【问题描述】:

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

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

下面是我的 viewDidLoad 代码。

顺便说一句,bins 是我在核心数据中的实体,mapView 是我的 IBOutlet 变量,连接到情节提要中的 Map Kit。

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

【问题讨论】:

    标签: swift xcode mapkit


    【解决方案1】:

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

    【讨论】:

    • 这很有帮助,谢谢。我只是想知道,如果我将动画设置为 false,它还会缩放吗?
    • 是的,animated 参数(true/false)只是控制缩放是瞬时的还是平滑的动画过渡,而不是是否发生。
    猜你喜欢
    • 2019-03-30
    • 2014-02-22
    • 2012-03-06
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多