UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)
        }
    }
}

2      

@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.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.