ios - 如何快速将正确的标注附件添加到自定义注释?

标签 ios swift mkmapview mkannotation

我有以下自定义注释类:

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

我正在将注释加载到这样的 map View 中:

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

如何在 map View 中的注释右侧添加 (i) 信息按钮。还有一种简单的方法来制作动画吗?

谢谢。

最佳答案

试试这个:

这里是实现 MKAnnotation 协议(protocol)的“示例”类。您需要根据注释类型进行调整。

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
}

关于ios - 如何快速将正确的标注附件添加到自定义注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317032/

相关文章:

ios - 如何在 swift 中使基类属性通用,以便与多种模型类型一起使用?

ios - 从ios中的经纬度获取地址

iphone - 如何在同一位置放置多个图钉

ios - 使用当前位置更新屏幕中心

ios - swift 启用使用 copy() 或 unshare method()

ios - 为什么在启用 NSZombie 时我会看到所有这些泄漏?

json - 日期字符串与格式化程序预期的格式不匹配

arrays - 如何从嵌套数组返回值 - swift 3

iphone - 无需更改即可调用函数“didUpdateToLocation”

ios - 如何在 Swift 中列出所有符合协议(protocol)的类?