我有以下自定义注释类:
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/