Как мы можем напечатать выбранную аннотацию на консоль? - PullRequest
0 голосов
/ 26 сентября 2018

Я новичок в swift, вот решение для добавления аннотации с помощью длинного прикосновения жестом. Как мы можем напечатать выбранную аннотацию на консоли?

Swift 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 голосов
/ 26 сентября 2018

Вы можете использовать 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.

...