Centering modes

This commit is contained in:
Garth Vander Houwen 2023-02-21 21:57:22 -08:00
parent da7576a650
commit 07ceaed23a
6 changed files with 49 additions and 23 deletions

View file

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

View file

@ -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") ?? ""
}
}

View file

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

View file

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

View file

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

View file

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