MKMapView 缩放到加载注释时出现问题

Trouble with MKMapView zoom to annotation on load

我在加载 MapView 时无法放大 annotation/pin。

加载 MapView 时,我希望它开始缩小,然后向 annotation/pin 放大。 "animated" 设置为 "true"。我不确定这里出了什么问题。

加载地图时,它会短暂显示一个带有图钉的灰色屏幕,然后加载它周围的卫星地图。

谢谢!

使用Swift 3

地图代码:

   @IBOutlet weak var mapView: MKMapView!

   }



   override func viewDidLoad() {
       super.viewDidLoad()


       let distanceSpan:CLLocationDegrees = 300
       let redRocks:CLLocationCoordinate2D = CLLocationCoordinate2DMake(39.665496, -105.205438)
       let redRocksPin = RedRocksPin(title: "Jerry Garcia 1/2/72", subtitle: "Red Rocks Amphitheatre", coordinate: redRocks)
       mapView.addAnnotation(redRocksPin)

       mapView.setRegion(MKCoordinateRegionMakeWithDistance(redRocks, distanceSpan, distanceSpan), animated: true)

引脚 Class:

import MapKit

class RedRocksPin: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D

init(title:String, subtitle:String, coordinate: CLLocationCoordinate2D) {
    self.title = title
    self.subtitle = subtitle
    self.coordinate = coordinate
}

在您的 class 声明中,将这些设为您的 class 变量:

    let distanceSpan:CLLocationDegrees = 300
    let redRocks:CLLocationCoordinate2D = CLLocationCoordinate2DMake(39.665496, -105.205438)

在 ViewWillAppear 中,执行此操作:

    override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animted)
       view.layoutIfNeeded()

       let redRocksPin = RedRocksPin(title: "Jerry Garcia 1/2/72", subtitle: "Red Rocks Amphitheatre", coordinate: redRocks)
       mapView.addAnnotation(redRocksPin)
    }

然后是这个:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        mapView.setRegion(MKCoordinateRegionMakeWithDistance(redRocks, distanceSpan, distanceSpan), animated: true)
    }

所以它所做的是在您的 viewwillAppear 中,它会绘制 mapView,因此您可以添加注释而不会导致崩溃,然后在 viewdidappear 中,它只会放大您添加的注释。

不确定这是否是最好的方法,但可以尝试一下。