Swift Framework MapKit

Main Idea

Maps have been a core feature of iPhone since the very first device shipped way back in 2007, and the underlying framework has been available to developers for almost as long. Even better, Apple provides a SwiftUI Map view that wraps up the underlying map framework beautifully, letting us place maps, annotations, and more alongside the rest of our SwiftUI view hierarchy. https://www.hackingwithswift.com/books/ios-swiftui/integrating-mapkit-with-swiftui

```swift

import MapKit import SwiftUI

struct Location: Identifiable { let id = UUID() let name: String let coordinate: CLLocationCoordinate2D }

struct ContentView: View { @State private var mapRegion = MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2) )

let locations = [
    Location(name: "Buckingham Palace", coordinate: CLLocationCoordinate2D(latitude: 51.501, longitude: -0.141)),
    Location(name: "Tower of London", coordinate: CLLocationCoordinate2D(latitude: 51.508, longitude: -0.076))
]

var body: some View {
    NavigationView {
        Map(coordinateRegion: $mapRegion, annotationItems: locations) { location in
            MapAnnotation(coordinate: location.coordinate) {
                NavigationLink {
                    Text(location.name)
                } label: {
                    Circle()
                        .stroke(.red, lineWidth: 3)
                        .frame(width: 44, height: 44)
                }
            }
        }
        .navigationTitle("London Explorer")
    }   
 } }

Notes mentioning this note


Here are all the notes in this garden, along with their links, visualized as a graph.