Swift,如何在点击时从自定义注释中获取信息
Swift, How to get information from a custom annotation on clicked
我有以下自定义注释 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)
}
我想从所选注释中获取 Location
对象。目前我有这个方法,每当我点击注释时都会调用它,但我不确定如何从注释中检索特定对象。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
print("Annotation selected")
//performSegueWithIdentifier("locationInfoSegue", sender: self)
}
谢谢。
您可以在 didSelectAnnotationView 中获取您的 Annotation,然后它将为您提供 MKAnnotationView。此 MKAnnotationView 将 MKAnnotation 作为对象。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
println("Annotation selected")
if let annotation = view.annotation as? LocationMapAnnotation {
println("Your annotation title: \(annotation.title)");
}
}
我有以下自定义注释 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)
}
我想从所选注释中获取 Location
对象。目前我有这个方法,每当我点击注释时都会调用它,但我不确定如何从注释中检索特定对象。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
print("Annotation selected")
//performSegueWithIdentifier("locationInfoSegue", sender: self)
}
谢谢。
您可以在 didSelectAnnotationView 中获取您的 Annotation,然后它将为您提供 MKAnnotationView。此 MKAnnotationView 将 MKAnnotation 作为对象。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
println("Annotation selected")
if let annotation = view.annotation as? LocationMapAnnotation {
println("Your annotation title: \(annotation.title)");
}
}