NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Dynamically pass coordinates to MapView with Pin

Forums > SwiftUI

I would like to create a map with a map pin as Paul Hudson describes in his post How to show annotations in a map View dated Feb 9, 2021. I am passing the coordinates dynamically through a call to MapView using CLLocationCoordinate2D instead of hardcoding the location.

struct DetailView: View {

var coordinate: CLLocationCoordinate2D {
     CLLocationCoordinate2D(
     latitude: item.entryLat,
     longitude: item.entryLong)
     }

     var body: some View {

     MapView(coordinate: coordinate)

In MapView I'm not sure what to do with my coordinates. Store them in the location structure?

struct MapView: View {

    var coordinate: CLLocationCoordinate2D
Here is the sample code from the article:
struct Location: Identifiable {
    let id = UUID()
    let name: String
    let coordinate: CLLocationCoordinate2D
}

 let annotations = [
        Location(name: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275)),
        Location(name: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8567, longitude: 2.3508)),
        Location(name: "Rome", coordinate: CLLocationCoordinate2D(latitude: 41.9, longitude: 12.5)),
        Location(name: "Washington DC", coordinate: CLLocationCoordinate2D(latitude: 38.895111, longitude: -77.036667))
    ]

struct ContentView: View {
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))

    var body: some View {
        Map(coordinateRegion: $region, annotationItems: annotations) {
            MapPin(coordinate: $0.coordinate)
        }
    }
}

   

@travelBuf No idea if you already solved this issue, I you did I’d like to know how you did it. In my case, I need to read in a lot (300+) locations from a server and display the pointers on the map. I found a work around, it’s probably not the best but it works for now.

In short my solution: Programmatically save the annotations information from the server in a local stored JSON file and use that file to display your annotations on the map. It’s most likely not the best solution but it works for me at the moment. I you require more info let me know, it’s just too much code to post here.

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.