获取注释图钉点击事件 MapKit Swift
Get annotation pin tap event MapKit Swift
我有一个 class 的数组。在 mkmapview 中,我附加了一些注释图钉。
var events = [Events]()
for event in events {
let eventpins = MKPointAnnotation()
eventpins.title = event.eventName
eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon)
mapView.addAnnotation(eventpins)
}
我用 map 的委托实现了一个函数
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print(view.annotation?.title! ?? "")
}
如何获取数组 events
中的哪一行被点击?
因为我想在另一个 ViewController 中继续,我想发送这个 Class 对象。
您应该创建自定义注释 class,例如:
class EventAnnotation : MKPointAnnotation {
var myEvent:Event?
}
然后,当您添加注释时,您将 link Event
带有自定义注释:
for event in events {
let eventpins = EventAnnotation()
eventpins.myEvent = event // Here we link the event with the annotation
eventpins.title = event.eventName
eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon)
mapView.addAnnotation(eventpins)
}
现在,您可以在委托函数中访问事件:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// first ensure that it really is an EventAnnotation:
if let eventAnnotation = view.annotation as? EventAnnotation {
let theEvent = eventAnnotation.myEvent
// now do somthing with your event
}
}
我有一个 class 的数组。在 mkmapview 中,我附加了一些注释图钉。
var events = [Events]()
for event in events {
let eventpins = MKPointAnnotation()
eventpins.title = event.eventName
eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon)
mapView.addAnnotation(eventpins)
}
我用 map 的委托实现了一个函数
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print(view.annotation?.title! ?? "")
}
如何获取数组 events
中的哪一行被点击?
因为我想在另一个 ViewController 中继续,我想发送这个 Class 对象。
您应该创建自定义注释 class,例如:
class EventAnnotation : MKPointAnnotation {
var myEvent:Event?
}
然后,当您添加注释时,您将 link Event
带有自定义注释:
for event in events {
let eventpins = EventAnnotation()
eventpins.myEvent = event // Here we link the event with the annotation
eventpins.title = event.eventName
eventpins.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLon)
mapView.addAnnotation(eventpins)
}
现在,您可以在委托函数中访问事件:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// first ensure that it really is an EventAnnotation:
if let eventAnnotation = view.annotation as? EventAnnotation {
let theEvent = eventAnnotation.myEvent
// now do somthing with your event
}
}