【发布时间】:2017-05-20 16:15:06
【问题描述】:
我正在制作一个使用 MKPointAnnotations 的 Swift 应用程序,我最近遇到了一个问题,我需要在注释中存储元数据,因此我创建了下面的自定义类:
class BRETTFAnnotation: MKPointAnnotation {
var tag: Int64
var name: String
init(lat : Double, lon:Double, t : Int64, n: String) {
self.tag = t
self.name = n
super.init()
self.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
}
我的MKAnnotationViewviewforMKAnnotation方法如下图:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let newAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "reuse")
newAnnotation.canShowCallout = true
let right = self.button(title: "Yes")
right?.addTarget(self, action: #selector(clickedToConfirmNewPoint), for: .touchUpInside)
newAnnotation.rightCalloutAccessoryView = right
let left = self.button(title: "No")
left?.addTarget(self, action: #selector(clickedToCancelNewPoint), for: .touchUpInside)
newAnnotation.leftCalloutAccessoryView = left
return newAnnotation
}
我遇到的问题是,当我点击我的自定义BRETTFAnnotation(我添加到我的MKMapView)时,什么也没有发生。当我只使用MKPointAnnotation(而不是BRETTFAnnotation)时,当我单击地图时,MKAnnotationView 上的两个按钮会显示。 我正在尝试使用我的BRETTFAnnotation 而不是MKPointAnnotation 让MKPinAnnotationView 在触摸时显示。
当用户同时点击注释时,如何继续使用我的自定义注释并显示标注?
编辑 1:因为它可能有用,所以下面的代码是我如何制作注释并将其添加到 mapView。
let location = gestureRecognizer.location(in: mapView)
let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
print("adding lat,long \(coordinate.latitude),\(coordinate.longitude)")
lastPoint = BRETTFAnnotation(lat: coordinate.latitude, lon: coordinate.longitude, t: 1, n: "")
let annotationView = MKPinAnnotationView(annotation: lastPoint, reuseIdentifier: "reuse")
mapView.addAnnotation(lastPoint)
【问题讨论】:
-
你到底想做什么?使用 mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 来处理注解的点击。
-
@kuzdu 我以前没有使用过,但是现在当我点击我的自定义注释时,标注没有显示。我只是尝试使用任何方法处理自定义注释的点击,如果您引用的方法可以工作,请解释如何。
标签: swift mapkit mkannotation mkpinannotationview