如何在MKMapView中检测引脚上的Tap and Hold?

时间:2014-02-12 00:20:13

标签: mkmapview mkplacemark

标题说明了一切。我正试图检测MKMapView中针脚的敲击,我甚至不知道从哪里开始。它不是UIView,所以我无法添加手势识别器,我在UIView中找不到MKPlaceMark来添加它。

1 个答案:

答案 0 :(得分:1)

你的问题不是很清楚,但你可以这样做:

    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
    }
}

您可以在Thorn web site

中了解更多详情