tap on map and get location

Will this version of MapKit allow you to tap on a map and get the location to say play a pin there?

Thanks

Post not yet marked as solved Up vote post of Tomha Down vote post of Tomha
1.7k views

Replies

The onTapGesture that returns a point is deprecated and only return screen coord.

So my question is how to take the x,y and get Lat/Long

Multiline

VStack { Map() // .onTapGesture { // print("onTapGesture") // } // .onTapGesture(perform: { // print ("onTapperform") // }) .onTapGesture(perform: { pt in print(pt) })

BlockQuote

The current version I could add MKMapViewDelegate to my MapView

Multiline

@objc func handleTap(sender: UITapGestureRecognizer) { guard sender.state == .ended else { return } let locationInView = sender.location(in: parent.vm.mapView) let tappedCoordinate = parent.vm.mapView.convert(locationInView, toCoordinateFrom: parent.vm.mapView) }

BlockQuote

Can you provide me the iOS17/Xcode 15?

I put together a little "hello MapKit" application to try with.

The idea is click the "Place A Pin" button, then the next spot, on them map clicked, a pin should appear.

My issue is HOW TO convert the X/Y position of the screen to Lat/Lon potions for CLLocation object.

Any body know how to do this?

Here is the GITHub Repo https://github.com/tomha2014/LearnMapKitiOS17

Thanks

Apple answer my TSI and it worked :)

You need to wrap map with MapReader here is a simple example:

`

               MapReader{ reader in
                    Map(
                        position: $cameraProsition,
                        interactionModes: .all
                    )
                    {}
                    .onTapGesture(perform: { screenCoord in
                        pinLocation = reader.convert(screenCoord, from: .local)
                    })`

Here is my learning project: https://github.com/tomha2014/LearnMapKitiOS17.git

I figured out something similar, but I am having issues that the latitude of the coordinates seem to be wrong on macOS at least. Basically my markers appear a bit below the clicked coordinate (unless I turn off the toolbar, in which case they appear at the mouse location but we are talking about the top of the balloons, not the little bottom arrow part).

I suspect there is an issue with converting the coordinates using .local caused by the height off toolbars and the window frame.

It seems I get the correct latitude if I hide the toolbar and expand the window to fullscreen.

  • Just FYI, in the following case on macOS, you need to embed the navigation view in a geometry reader to get the safe area insets and subtract the top part from the tap y coordinate:

    struct ContentView: View { var body: some View { NavigationView { MyLeftSideView() MyMapView() } } }

    And in the following case, coors are correctly calculated without caring about the safe area inset:

    struct ContentView: View { var body: some View { MyMapView() } }
Add a Comment