Learn Courses My Dashboard

Is there a way to click/tap an MKAnnotationView ONLY once and be redirected to another view?

I have to tap an MKannotationView TWICE to be redirected…

is there a way to click MKannotationView only once and be redirected?

current behavior:
-tap into a MKannotationView
-shows bubble with annotation view details
-click bubble and I’m redirected to view with details

I’d like this behavior:
-tap into a MKannotationView
-I’m redirected to view with details

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            
            if annotation is MKUserLocation {
                return nil
            }
            
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "listings")
            
            if annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "listings")
                let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
                lbl.backgroundColor = .blue
                lbl.textColor = .white
                lbl.tag = 42
                annotationView!.addSubview(lbl)
                annotationView!.canShowCallout = true
                annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                annotationView!.frame = lbl.frame
            } else {
                annotationView!.annotation = annotation
            }
            
            let lbl = annotationView!.viewWithTag(42) as! UILabel
            lbl.text = annotation.subtitle!
            
            return annotationView
        }
        
        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            
            for listingHere in map.model.results!.listings {
                if listingHere.address == view.annotation?.title {
                    map.selectedListing = listingHere 
                    return
                }
            }
            
            
        }

Interesting question! I wondered the same thing myself. Apparently these people found the trick four years ago: swift - MKPointAnnotation go to another view - Stack Overflow

However, I couldn’t get it to work in my own code.

Starting in Xcode 12, SwiftUI can also support maps out of the box. If you wanted to refactor, you might find a way. Still, I haven’t seen too much out there on this, and couldn’t figure out how to do it in from the docs

Sorry, I couldn’t be more help!! If you find the answer, please share it as well. Or perhaps someone else here can try this.

Yes I made it work using the ios15 Map. It was so freaking easy. I ditch the UIKIT completely

A good example is here:

1 Like

Hey that’s great news!! Well done :clap: :champagne:

Nice article you posted as well! Made it clear and easy to update maps, it’s so much better than deciphering the docs for UIKit, or needing to create all of those delegate methods.

Also, I loved at the end how they showed exactly what you wanted to do: jump to a different View in a single click through a NavigationLink:

MapAnnotation(coordinate: place.coordinate) {
  NavigationLink {
    LocationDetailsView(place: place)
  } label: {
    PlaceAnnotationView(title: place.name)
  }
}