如何在 swift 中为自定义注释添加正确的标注附件?

How to add right callout accessory to custom annotation in swift?

我有以下自定义注释 class:

import UIKit
import MapKit

class LocationMapAnnotation: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var location: Location

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

我正在将注释加载到这样的地图视图中:

for i in 0..<allLocations.count{
            //Add an annotation
            let l: Location = self.allLocations[i] as! Location
            let coordinates = CLLocationCoordinate2DMake(l.latitude as Double, l.longitude as Double)
            let annotation = LocationAnnotation(title: l.name, coordinate: coordinates, location: l)
            mapView.addAnnotation(annotation)
        }

如何在地图视图中的注释右侧添加 (i) 信息按钮。有没有简单的动画方法?

谢谢。

试试这个:

此处"Sample" class 实现了 MKAnnotation 协议。您需要根据您的注释类型进行调整。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Sample"

if annotation.isKindOfClass(Sample.self) {
    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {

 // Reuse Annotationview

        annotationView.annotation = annotation
        return annotationView
    } else {

 // Create Annotation

        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
        annotationView.enabled = true
        annotationView.canShowCallout = true

 // Here I create the button and add in accessoryView

        let btn = UIButton(type: .DetailDisclosure)
        annotationView.rightCalloutAccessoryView = btn
        return annotationView
    }
}
return nil
}