MapKit Location Picker
Interactive location picker using MapKit

Preview

Code
1import SwiftUI2import MapKit34struct LocationPickerView: View {5 @State private var region = MKCoordinateRegion(6 center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),7 span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)8 )9 @State private var selectedLocation: CLLocationCoordinate2D?10 11 var body: some View {12 VStack {13 Text("Select Location")14 .font(.title2)15 .fontWeight(.semibold)16 .padding(.bottom)17 18 Map(coordinateRegion: $region, annotationItems: selectedLocation.map { [$0] } ?? []) { location in19 MapAnnotation(coordinate: location) {20 VStack {21 Image(systemName: "mappin.circle.fill")22 .font(.title)23 .foregroundColor(.red)24 Text("Selected")25 .font(.caption)26 .padding(4)27 .background(Color.white)28 .cornerRadius(4)29 }30 }31 }32 .frame(height: 300)33 .cornerRadius(12)34 .onTapGesture { location in35 let coordinate = region.center36 selectedLocation = coordinate37 }38 39 if let location = selectedLocation {40 VStack(alignment: .leading, spacing: 8) {41 Text("Selected Location:")42 .font(.headline)43 Text("Lat: \(location.latitude, specifier: "%.4f")")44 Text("Lng: \(location.longitude, specifier: "%.4f")")45 }46 .padding()47 .background(Color(.systemGray6))48 .cornerRadius(8)49 }50 }51 .padding()52 }53}5455struct MapLocation: Identifiable {56 let id = UUID()57 let coordinate: CLLocationCoordinate2D58}5960struct LocationPickerView_Previews: PreviewProvider {61 static var previews: some View {62 LocationPickerView()63 }64}