Question

The title says it all. I'm trying to detect a tap on a pin in a MKMapView and I don't even know where to begin. Its not an UIView, so I can't add a gesture recognizer and I can't find a UIView in MKPlaceMark to add it.

No correct solution

OTHER TIPS

You're question is not very clear but you can do like this:

    import UIKit
    import MapKit

    protocol HandleMapSearch: class {
        func dropPinZoomIn(placemark:MKPlacemark)
    }

    class ViewController: UIViewController {

       func getDirections(){

           // Here you can put anythings like:

        guard let selectedPin = selectedPin else { return }
        let mapItem = MKMapItem(placemark: selectedPin)
        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
        mapItem.openInMapsWithLaunchOptions(launchOptions)
    }
    }

extension ViewController : MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{       

        guard !(annotation is MKUserLocation) else { return nil }
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        }
        pinView?.pinTintColor = UIColor.orangeColor()    // The pin's color
        pinView?.canShowCallout = true    // To set dialogue bubbles of the pin.

        let smallSquare = CGSize(width: 30, height: 30)
        let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))       // To initialize the button in the dialogue bubbles of the pin.

        button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)    // To set and initialize the button.
        pinView?.leftCalloutAccessoryView = button

        return pinView
    }
}

You can have more details in Thorn web site

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top