0

I am trying to get SwiftUI + MapKit + LongPress gesture to work. When I add the map to the ContentView works great. I then add the .onLongPressGesture handler to the map, and the panning/zooming stops working. LONG PRESS WORKS though!

You can see my sample project at: https://github.com/tomha2014/KISS_MapKit_SwiftUI

but it goes like this:

           Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
//                .onLongPressGesture {
//                    // How do I get the location (Lat/Long) I am pressed on?
//                    print("onLongPressGesture")
//                }

Also any body know how to get the lat/long when the press was made?

I think I could do this with a map controller, but this seems to be the "Modern" way, I just wished it was the document way.

Thanks Tom

1 Answers1

1

Don't ask why but this seems to work:

    Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
        .gesture(DragGesture())
        .onLongPressGesture {
            print("Here!")
        }
ChrisR
  • 3,983
  • 1
  • 3
  • 17
  • Based on ChrisR gesture I looked into using gesture I think this is the right way to do it `Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true) .gesture(DragGesture()) .gesture( LongPressGesture() .onEnded { value in print("LongPressGesture "+value.description) } )` Still can not figure out how to get lat/long, Any Ideas? I know you take the x,y of the screen and convert to lat/long – tom hackbarth Mar 09 '22 at 14:33
  • getting the tap/longpress location unfortunately is not that easy in SwiftUI ... start from here: https://stackoverflow.com/questions/56513942/how-to-detect-a-tap-gesture-location-in-swiftui – ChrisR Mar 09 '22 at 16:14
  • Once you have the press location you can relate that you your currently shown coordinateRegion and recalculate into lat/lon. – ChrisR Mar 09 '22 at 16:15
  • btw: a much simpler approach would be to have some fixed circle or crosshairs in the center of the screen which sets a new pinned location on button press. btw2: SwiftUI's Map View is very very basic compared to UIKit. – ChrisR Mar 09 '22 at 16:17