class 扩展未被调用 - 无法在地图上配置注释

class extension not being called - unable to configure annotations on map

我在 Swift 5 和 Xcode 11,目前正在尝试使用自定义图像而不是默认标记创建带有预定义注释的地图。我一直在研究 Apple 的 AnnotatingMapWithCustomData 示例代码,并且可以根据我的需要对其进行调整。但是一旦我尝试将代码复制到我自己的项目中,注释就会显示为默认标记,而不是我配置的自定义注释视图。

import UIKit
import MapKit

class MapViewController: UIViewController {

    @IBOutlet private weak var mapView: MKMapView!

    private var allAnnotations: [MKAnnotation]?

    private var displayedAnnotations: [MKAnnotation]? {
        willSet {
            if let currentAnnotations = displayedAnnotations {
                mapView.removeAnnotations(currentAnnotations)
            }
        }
        didSet {
            if let newAnnotations = displayedAnnotations {
                mapView.addAnnotations(newAnnotations)
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        registerMapAnnotationViews()

        // Create the array of annotations and the specific annotations for the points of interest.
        allAnnotations = [ShrekAnnotation(), CoffeeAnnotation()]

        // Dispaly all annotations on the map.
        displayedAnnotations = allAnnotations
        centerMapOnLondon()
    }

    /// - Tag: RegisterAnnotationViews
    private func registerMapAnnotationViews() {
        mapView.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: NSStringFromClass(ShrekAnnotation.self))
        mapView.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: NSStringFromClass(CoffeeAnnotation.self))
    }

    private func centerMapOnLondon() {
        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let center = CLLocationCoordinate2D(latitude: 51.507911, longitude: -0.132222)
        mapView.setRegion(MKCoordinateRegion(center: center, span: span), animated: true)
    }
}

extension MapViewController: MKMapViewDelegate {

    /// - Tag: CreateAnnotationViews
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !annotation.isKind(of: MKUserLocation.self) else {
            // Make a fast exit if the annotation is the `MKUserLocation`, as it's not an annotation view we wish to customize.
            return nil
        }

        var annotationView: MKAnnotationView?

        if let annotation = annotation as? ShrekAnnotation {
            annotationView = setupShrekAnnotationView(for: annotation, on: mapView)
        } else if let annotation = annotation as? CoffeeAnnotation {
            annotationView = setupCoffeeAnnotationView(for: annotation, on: mapView)
        }

        return annotationView
    }

    /// - Tag: ConfigureAnnotationViews
    private func setupShrekAnnotationView(for annotation: ShrekAnnotation, on mapView: MKMapView) -> MKAnnotationView {
        let reuseIdentifier = NSStringFromClass(ShrekAnnotation.self)
        let view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier, for: annotation)

        view.canShowCallout = true

        // Provide the annotation view's image.
        let image = #imageLiteral(resourceName: "map-shrek1")
        view.image = image

        return view
    }

    /// - Tag: ConfigureAnnotationViews
    private func setupCoffeeAnnotationView(for annotation: CoffeeAnnotation, on mapView: MKMapView) -> MKAnnotationView {
        let reuseIdentifier = NSStringFromClass(CoffeeAnnotation.self)
        let view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier, for: annotation)

        view.canShowCallout = true

        // Provide the annotation view's image.
        let image = #imageLiteral(resourceName: "map-cof1")
        view.image = image

        return view
    }
}

显然 class 扩展没有被调用,但我不知道如何解决这个问题。在我尝试在我自己的项目中实现它之前工作的代码是相同的。我是否遗漏了一些我应该复制的东西,或者你能找到代码中错误的原因吗?

感谢您的帮助。

我认为您没有设置 mapView 委托,因此 viewFor annotation 没有被触发并且您的自定义注释视图没有出现在地图上。集

mapView.delegate = self

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    registerMapAnnotationViews()

    // Create the array of annotations and the specific annotations for the points of interest.
    allAnnotations = [ShrekAnnotation(), CoffeeAnnotation()]

    // Dispaly all annotations on the map.
    displayedAnnotations = allAnnotations
    centerMapOnLondon()
}

希望对您有所帮助