我们如何将选定的注释打印到控制台?

时间:2018-09-26 15:18:57

标签: ios swift mkmapview

我是新手,这是通过长手势触摸添加注释的解决方案,我们如何将选定的注释打印到控制台?

快捷键4

import UIKit
import MapKit

class MapViewController: UIViewController , MKMapViewDelegate {

    @IBOutlet weak var mapView : MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()


    let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(MapViewController.handleTap(_:)))
    longPressRecogniser.minimumPressDuration = 1.0
    mapView.addGestureRecognizer(longPressRecogniser)

    mapView.mapType = MKMapType.standard

    let location = CLLocationCoordinate2D(latitude: 23.0225,longitude: 72.5714)

    let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "iOSDevCenter-Kirit Modi"
    annotation.subtitle = "Ahmedabad"
    mapView.addAnnotation(annotation)
}

这是添加注释的功能:

    @objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
{

    let location = gestureReconizer.location(in: mapView)
    let coordinate = mapView.convert(location,toCoordinateFrom: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

1 个答案:

答案 0 :(得分:2)

您可以使用mapView:didSelect方法。

例如

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    print("selected annotation", view.annotation)
    print("selected annotation", view.annotation?.title)
    print("selected annotation", view.annotation?.coordinate)
}

不要忘记在mapView.delegate = self中设置viewDidLoad