import MapKit import SwiftUI struct AttachmentLocationPickerView: View { @StateObject var locationManager = LocationManager() @State private var region = MKCoordinateRegion() @State private var userInteraction = false var body: some View { ZStack { // MapView MapView( region: $region, userInteraction: $userInteraction ) // Track button let img = userInteraction ? "location.circle" : "location.circle.fill" VStack { Spacer() HStack { Spacer() Image(systemName: img) .resizable() .frame(width: 30, height: 30) .foregroundColor(.Material.Elements.active) .background(Color.Material.Shape.white) .clipShape(Circle()) .padding(16) .shadow(color: .white, radius: 2) .tappablePadding(.symmetric(10)) { userInteraction = false } } } } .onAppear { locationManager.start() } .onChange(of: locationManager.region) { region in if !userInteraction { self.region = region } } } } struct MapView: UIViewRepresentable { @Binding var region: MKCoordinateRegion @Binding var userInteraction: Bool func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator mapView.showsUserLocation = false mapView.userTrackingMode = .none return mapView } func updateUIView(_ uiView: MKMapView, context _: Context) { if uiView.region != region, !userInteraction { uiView.setRegion(region, animated: true) } } func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, MKMapViewDelegate { var parent: MapView init(_ parent: MapView) { self.parent = parent } } } class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { private let locationManager = CLLocationManager() @Published var region: MKCoordinateRegion override init() { region = MKCoordinateRegion() super.init() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest } func start() { locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func stop() { locationManager.stopUpdatingLocation() } func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let loc = locations.first { region = MKCoordinateRegion( center: loc.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002) ) } } } extension MKCoordinateRegion: Equatable { public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool { lhs.center.latitude == rhs.center.latitude && lhs.center.longitude == rhs.center.longitude && lhs.span.latitudeDelta == rhs.span.latitudeDelta && lhs.span.longitudeDelta == rhs.span.longitudeDelta } }