MKMapView MKPointAnnotation tap event

前端 未结 3 549
旧巷少年郎
旧巷少年郎 2020-11-29 20:17

I have a list of annotations (MKPointAnnotation). I have a UIViewController which is for the whole view, MKMapView implementing Controller, which I thought is useful for det

3条回答
  •  日久生厌
    2020-11-29 21:13

    Robs example for Swift 3:

    class MapViewController: UIViewController
    {
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            // Create map view in storyboard
            view.delegate = self
        }
    }
    
    extension MapViewController: MKMapViewDelegate
    {
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
        {
            let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotationView")
            annotationView.canShowCallout = true
            annotationView.rightCalloutAccessoryView = UIButton.init(type: UIButtonType.detailDisclosure)
    
            return annotationView
        }
    
        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
        {
            guard let annotation = view.annotation else
            {
                return
            }
    
            let urlString = "http://maps.apple.com/?sll=\(annotation.coordinate.latitude),\(annotation.coordinate.longitude)"
            guard let url = URL(string: urlString) else
            {
                return
            }
    
            UIApplication.shared.openURL(url)
        }
    }
    

提交回复
热议问题