我正在使用MapKit并且已经显示了五个位置,并且引脚也来了。现在,当我按下引脚注释时,我想要一个标注。我怎么能得到它?
以下是代码:
class MultipleAnnotationController: UIViewController {
let sec22 = Capital(title: "Sector 22", coordinate: CLLocationCoordinate2D(latitude: 30.7339583, longitude: 76.77261969999995), info: "Sector 22 Aroma")
let sec34 = Capital(title: "Sector 34", coordinate: CLLocationCoordinate2D(latitude: 30.7195549, longitude: 76.76394989999994), info: "Aakash Institute")
let mohali = Capital(title: "Mohali Office", coordinate: CLLocationCoordinate2D(latitude: 30.707318902730268, longitude: 76.70382329676794), info: "Office Location")
let rmalerkotla = Capital(title: "Malerkotla Home", coordinate: CLLocationCoordinate2D(latitude: 30.5232076, longitude: 75.88825079999992), info: "Home")
let delhi = Capital(title: "New Delhi", coordinate: CLLocationCoordinate2D(latitude: 28.7040592, longitude: 77.10249019999992), info: "Capital of India")
@IBOutlet weak var mapV: MKMapView!
override func viewDidLoad() {
mapV.addAnnotations([sec22, sec34, mohali, rmalerkotla, delhi])
super.viewDidLoad()
}
class Capital: NSObject, MKAnnotation {
var title: String?
var coordinate: CLLocationCoordinate2D
var info: String
init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
self.title = title
self.coordinate = coordinate
self.info = info
}
}
}
答案 0 :(得分:0)
您需要实施以下方法,
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "Pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
}
else {
pinView?.annotation = annotation
}
return pinView
}
不要忘记设置mapV.delegate
。
override func viewDidLoad() {
super.viewDidLoad()
mapV.addAnnotations([sec22, sec34, mohali, rmalerkotla, delhi])
mapV.delegate = self
}