From 07ceaed23abb24249f0bdb03ff546494e684d37e Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 21 Feb 2023 21:57:22 -0800 Subject: [PATCH] Centering modes --- Meshtastic/Enums/AppSettingsEnums.swift | 11 ++--- Meshtastic/Model/UserSettings.swift | 7 ++++ .../Views/Map/Custom/MapViewSwiftUI.swift | 41 +++++++++++++------ Meshtastic/Views/Nodes/NodeDetail.swift | 4 +- Meshtastic/Views/Nodes/NodeMap.swift | 7 +++- Meshtastic/Views/Settings/AppSettings.swift | 2 +- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/Meshtastic/Enums/AppSettingsEnums.swift b/Meshtastic/Enums/AppSettingsEnums.swift index 32beae6d..7d17601d 100644 --- a/Meshtastic/Enums/AppSettingsEnums.swift +++ b/Meshtastic/Enums/AppSettingsEnums.swift @@ -39,21 +39,18 @@ enum CenteringMode: Int, CaseIterable, Identifiable { case allAnnotations = 0 case allPositions = 1 - case latestPosition = 2 - case clientGps = 7 + case clientGps = 2 var id: Int { self.rawValue } var description: String { get { switch self { case .allAnnotations: - return "Center of All Annotations"// NSLocalizedString("default", comment: "Default Keyboard") + return "All Annotations"// NSLocalizedString("default", comment: "Default Keyboard") case .allPositions: - return "Center of All Node Postions"// NSLocalizedString("ascii.capable", comment: "ASCII Capable Keyboard") - case .latestPosition: - return "Latest Node Position"//NSLocalizedString("twitter", comment: "Twitter Keyboard") + return "All Node Postions"// NSLocalizedString("ascii.capable", comment: "ASCII Capable Keyboard") case .clientGps: - return "Client GPS Location"//NSLocalizedString("email.address", comment: "Email Address Keyboard") + return "Client GPS"//NSLocalizedString("email.address", comment: "Email Address Keyboard") } } } diff --git a/Meshtastic/Model/UserSettings.swift b/Meshtastic/Model/UserSettings.swift index e7c4846d..45a161c0 100644 --- a/Meshtastic/Model/UserSettings.swift +++ b/Meshtastic/Model/UserSettings.swift @@ -45,6 +45,12 @@ class UserSettings: ObservableObject { UserDefaults.standard.set(meshMapType, forKey: "meshMapType") } } + @Published var meshMapCenteringMode: Int { + didSet { + UserDefaults.standard.set(meshMapCenteringMode, forKey: "meshMapCenteringMode") + UserDefaults.standard.synchronize() + } + } @Published var meshMapCustomTileServer: String { didSet { UserDefaults.standard.set(meshMapCustomTileServer, forKey: "meshMapCustomTileServer") @@ -60,6 +66,7 @@ class UserSettings: ObservableObject { self.provideLocationInterval = UserDefaults.standard.object(forKey: "provideLocationInterval") as? Int ?? 900 self.keyboardType = UserDefaults.standard.object(forKey: "keyboardType") as? Int ?? 0 self.meshMapType = UserDefaults.standard.string(forKey: "meshMapType") ?? "standard" + self.meshMapCenteringMode = UserDefaults.standard.object(forKey: "meshMapCenteringMode") as? Int ?? 0 self.meshMapCustomTileServer = UserDefaults.standard.string(forKey: "meshMapCustomTileServer") ?? "" } } diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 0331b883..62396ec2 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -16,9 +16,10 @@ struct MapViewSwiftUI: UIViewRepresentable { var onLongPress: (_ waypointCoordinate: CLLocationCoordinate2D) -> Void var onWaypointEdit: (_ waypointId: Int ) -> Void let mapView = MKMapView() - dynamic let positions: [PositionEntity] - dynamic let waypoints: [WaypointEntity] - dynamic let mapViewType: MKMapType + let positions: [PositionEntity] + let waypoints: [WaypointEntity] + let mapViewType: MKMapType + let centeringMode: CenteringMode let centerOnPositionsOnly: Bool @@ -33,16 +34,27 @@ struct MapViewSwiftUI: UIViewRepresentable { func makeUIView(context: Context) -> MKMapView { // Parameters - mapView.addAnnotations(waypoints) - if centerOnPositionsOnly { - mapView.fit(annotations: positions, andShow: true) - } else { - mapView.addAnnotations(positions) - mapView.fitAllAnnotations() - } mapView.mapType = mapViewType - mapView.setUserTrackingMode(.none, animated: true) + mapView.addAnnotations(waypoints) + // Logic to manage the map centering options + switch centeringMode { + case .allAnnotations: + mapView.addAnnotations(positions) + mapView.fitAllAnnotations() + case .allPositions: + mapView.fit(annotations: positions, andShow: true) + case .clientGps: + + let span = MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001) + let center = CLLocationCoordinate2D(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude) + let region = MKCoordinateRegion(center: center, span: span) + mapView.setRegion(region, animated: true) + mapView.setUserTrackingMode(.followWithHeading, animated: true) + mapView.addAnnotations(positions) + } + // Other MKMapView Settings + mapView.showsUserLocation = true mapView.preferredConfiguration.elevationStyle = .realistic mapView.isPitchEnabled = true mapView.isRotateEnabled = true @@ -52,7 +64,7 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.showsCompass = true mapView.showsScale = true mapView.showsTraffic = true - mapView.showsUserLocation = true + #if targetEnvironment(macCatalyst) mapView.showsZoomControls = true mapView.showsPitchControl = true @@ -90,6 +102,11 @@ struct MapViewSwiftUI: UIViewRepresentable { } DispatchQueue.main.async { + if centeringMode == CenteringMode.clientGps { + let span = MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001) + let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude), span: span) + mapView.setRegion(region, animated: true) + } mapView.removeAnnotations(mapView.annotations) mapView.addAnnotations(positions) mapView.addAnnotations(waypoints) diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index 652852e0..9e967ed7 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -67,7 +67,9 @@ struct NodeDetail: View { editingWaypoint = wpId presentingWaypointForm = true } - }, positions: annotations, waypoints: Array(waypoints), mapViewType: mapType, + }, positions: annotations, waypoints: Array(waypoints), + mapViewType: mapType, + centeringMode: .allPositions, centerOnPositionsOnly: true, customMapOverlay: self.customMapOverlay, overlays: self.overlays diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index 45fa6765..008c5d2b 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -30,6 +30,7 @@ struct NodeMap: View { } } @AppStorage("meshMapType") private var meshMapType = "hybridFlyover" + @AppStorage("meshMapCenteringMode") private var meshMapCenteringMode = 0 //&& nodePosition != nil @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "time", ascending: true)], @@ -43,6 +44,7 @@ struct NodeMap: View { private var waypoints: FetchedResults @State private var mapType: MKMapType = .standard + @State private var mapCenteringMode: CenteringMode = .allAnnotations @State var waypointCoordinate: CLLocationCoordinate2D = LocationHelper.DefaultLocation @State var editingWaypoint: Int = 0 @State private var presentingWaypointForm = false @@ -73,7 +75,8 @@ struct NodeMap: View { } }, positions: Array(positions), waypoints: Array(waypoints), - mapViewType: mapType , + mapViewType: mapType, + centeringMode: mapCenteringMode, centerOnPositionsOnly: false, customMapOverlay: self.customMapOverlay, overlays: self.overlays @@ -111,7 +114,7 @@ struct NodeMap: View { .onAppear(perform: { self.bleManager.context = context self.bleManager.userSettings = userSettings - + mapCenteringMode = CenteringMode(rawValue: meshMapCenteringMode) ?? CenteringMode.allAnnotations switch meshMapType { case "standard": mapType = .standard diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index e478e57c..4113945f 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -71,7 +71,7 @@ struct AppSettings: View { } Section(header: Text("mesh map options")) { - Picker("map.centering", selection: $userSettings.meshMapType) { + Picker("map.centering", selection: $userSettings.meshMapCenteringMode) { ForEach(CenteringMode.allCases) { cm in Text(cm.description) }