地图中的点按或可点击注释标题。例如,我有代码:
@IBOutlet var Map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
Map.delegate = self
let location = CLLocationCoordinate2DMake(lat!, lon!)
let span = MKCoordinateSpanMake(0.1, 0.1)
let region = MKCoordinateRegion(center: location, span: span)
Map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Title"
Map.addAnnotation(annotation)
}
我尝试使用UITapGestureRecognizer
和mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
,但它不起作用。也许有人有任何想法
答案 0 :(得分:1)
实施委托方法。
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
let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
pinView?.annotation = annotation
}
return pinView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print(#function)
if control == view.rightCalloutAccessoryView {
// Do something
}
}