Taking user input in MapKit

Is there an easy way to take in user input through a tap gesture and place a marker in a Map using MapKit?

I am not very experienced in SwiftUI. The only thing that comes to my mind is creating an overlay, detecting the tap position, converting that into map coordinates using map center and span, and then creating a MapPin for that. Only problem is, because of the overlay I can't pan or zoom the map.

I can add drag gestures to the overlay and then pass the translation to map in the form of map center, but that's too much work.

Is there any better way to do this?

Your solution can be the extra work. Google, search GitHub or reach the Apple Documentation, look at the sample code.

Begin the base knowledge here: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW1

Converting MKView touch to coordinates: https://gist.github.com/a1phanumeric/2249553

You have not said what platform you are developing for, but let's assume iOS (or iPadOS, but the same principles apply to macOS).

If you are serious about using MapKit, and want to go beyond very basic mapping functions... ...then handle your map using UIKit, not SwiftUI.

Mapping in SwiftUI is still lacking a lot of features which you may need, and if you start off using SwiftUI, then run into it's limits, you may have to begin again.

I love SwiftUI, but it's mapping is too weak.

On macOS I use Cocoa (NSViewController) for maps, wrapped in an NSViewControllerRepresentable.
I found problems when using NSViewRepresentable, so I don't use that. (On iOS, the equivalent to NSViewControllerRepresentable is UIViewControllerRepresentable.)

If anyone disagrees with this, then I would love to hear their better solutions!

Taking user input in MapKit
 
 
Q