如何在不同位置设置不同的图像图钉 SWIFT

How to set DIFFERENT image pins in different locations SWIFT

我正在尝试编写一个在 AppleMap 上显示不同图像的应用程序。我成功设置了一个图片pin,但我不知道如何设置不同的图片。

在我看来 didLoad 我写道:

self.pin = AnnotationPin(title: sport, subtitle: "ouiii", coordinate: coordinate)
                                                            
self.MyMapView.addAnnotation(self.pin)

在 viewcontroller 我有:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
    guard !(annotation is MKUserLocation) else {
        return nil
    }

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
       
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "kite")
    }

    return annotationView
}

我制作了另一个名为注释的 swift 文件,其中包含:

class AnnotationPin:  NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?

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

我已经为字幕和标题调用了这个 class,我想我可以再次调用它来选择我的图像。但是我不知道该怎么做,也没有找到其他主题的答案。

您可以将 image 属性 添加到您的 AnnotationPin class 中,然后在 viewForAnnotation 中使用条件向下转换来查看您是否正在交易与您的注释之一。如果你是那么你可以使用 image 属性

class AnnotationPin:  NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?
let image: UIImage?

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


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
    guard let myAnnotation = annotation as? AnnotationPin  else {
        return nil
    }

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
       
        annotationView.canShowCallout = true
        annotationView.image = myAnnotation.image
    }

    return annotationView
}