Learn Courses My Dashboard

MapKit catch tap and place marker

Need some code to handle taps on a map. The following spells it out. The functions are boilerplate from the CItySights project (6/14)

    var locations:[MKPointAnnotation] {
        var annotations = [MKPointAnnotation]()
        // if nothing here, produces general north America map... OK for starters.
        // user then manipulates map and taps on a location
        // need code to get 'TAP' location, retrieve map coordinates, and add pin or marker
        // how do we do this, keeping it simple??
        // i.e. don't need user location, nearby business, fancy gesture detection, etc.
        return annotations
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.mapType = .mutedStandard
        mapView.showsUserLocation = false
        return mapView
    func updateUIView(_ uiView: MKMapView, context: Context) {
        uiView.showAnnotations(self.locations, animated: true)
    static func dismantleUIView(_ uiView: MKMapView, coordinator: ()) {

It looks like I need to elaborate a bit. The modern UIView setup was introduced to me with Chris’ CitySights app, and I have implemented it on other map views with some success where I already have the coordinates. By modern I’m alluding to XCode 12 and IOS 14.
All I’m looking for is a way to integrate a user tap function into the UIView methods, that will give me the geographic coordinates of the tap. This seems to me it should be simple. I tried to get it going using Apple Developer references, but haven’t been able to figure it out yet.
If I search the web, I seem to get only outdated info involving storyboards, gesture controllers using deprecated functions and variables, etc. This seems an unnecessary step backwards. Perhaps the CitySights Coordinator might be a good place to implement a tap recognition routine. Here’s the Coordinator as I have it on the mapView:

    class Coordinator: NSObject, MKMapViewDelegate {
        var map: EditMap
        init(map: EditMap) {
            self.map = map
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            // If the annotation is the user blue dot, return nil
            if annotation is MKUserLocation {
                return nil
            // Check if there's a reusable annotation view first
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: Constants.annotationReuseId)
            if annotationView == nil {
                // Create a new one
                annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constants.annotationReuseId)
                annotationView!.canShowCallout = true
                annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            else {
                // We got a resuable one
                annotationView!.annotation = annotation
            // Return it
            return annotationView

Any way, I’m stumped (dare I say tapped out?) Please help.

maybe this can help