SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location

By ChatGPT, Dalle 3What are some scenarios we might want to convert screen coordinates to actual geographical location on the map?For example, we want our user to be able to select any location on the map and create a new marker at that location repres…


This content originally appeared on Level Up Coding - Medium and was authored by Itsuki

By ChatGPT, Dalle 3

What are some scenarios we might want to convert screen coordinates to actual geographical location on the map?

For example, we want our user to be able to select any location on the map and create a new marker at that location representing their favorite spot!

Thanks to MapReader and MapProxy, we can get that in couple lines of code!

(That is this article is going to be SHORT!)

All we have to do is to wrap our Map within a MapReader, and use this MapProxy object created by it that is able to convert screen locations to map locations and back the other way.

You can also use this proxy to convert between a MapCamera and a MKMapRect or MKCoordinateRegion , but I will leave that out for now!

Let’s see it in an example where we will show a Marker on where ever the user taps on.

import SwiftUI
import MapKit

private struct DemoView: View {
static let aspen = MapCameraPosition.camera(MapCamera(
centerCoordinate: CLLocationCoordinate2D(latitude: 39.1911, longitude: -106.817535),
distance: 500,
heading: 0,
pitch: 0
))

@State private var position: MapCameraPosition = Self.aspen
@State private var userTapLocation: CLLocationCoordinate2D?

var body: some View {
MapReader { proxy in
Map(position: $position) {
if let userTapLocation = userTapLocation {
Marker("tapped!", coordinate: userTapLocation)
.tint(.orange)
}
}
.onTapGesture { position in
if let coordinate = proxy.convert(position, from: .local) {
self.userTapLocation = coordinate
}
}
}

}
}

That’s it!

Thank you for reading!

Happy marking!


SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Itsuki


Print Share Comment Cite Upload Translate Updates
APA

Itsuki | Sciencx (2024-08-25T15:16:42+00:00) SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location. Retrieved from https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/

MLA
" » SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location." Itsuki | Sciencx - Sunday August 25, 2024, https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/
HARVARD
Itsuki | Sciencx Sunday August 25, 2024 » SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location., viewed ,<https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/>
VANCOUVER
Itsuki | Sciencx - » SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/
CHICAGO
" » SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location." Itsuki | Sciencx - Accessed . https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/
IEEE
" » SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location." Itsuki | Sciencx [Online]. Available: https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/. [Accessed: ]
rf:citation
» SwiftUI+MapKit: Convert Screen Coordinates to Actual Geographical Location | Itsuki | Sciencx | https://www.scien.cx/2024/08/25/swiftuimapkit-convert-screen-coordinates-to-actual-geographical-location/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.