From b8db2411101e81cbed00cc52779817d01152d685 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 20 Mar 2023 17:49:29 -0700 Subject: [PATCH] Route lines for the mesh map --- .../Views/Map/Custom/MapViewSwiftUI.swift | 36 ++++++++++++------- Meshtastic/Views/Nodes/NodeMap.swift | 3 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 62471651..616ee1de 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -30,7 +30,7 @@ struct MapViewSwiftUI: UIViewRepresentable { var customMapOverlay: CustomMapOverlay? @State private var presentCustomMapOverlayHash: CustomMapOverlay? - //let dynamicRegion: Bool = true + let colors: [UIColor] = [UIColor.systemIndigo, UIColor.blue, UIColor.purple, UIColor.green, UIColor.brown, UIColor.purple, UIColor.systemMint, UIColor.cyan, UIColor.magenta, UIColor.systemPink] func makeUIView(context: Context) -> MKMapView { // Map View Parameters @@ -110,6 +110,9 @@ struct MapViewSwiftUI: UIViewRepresentable { } DispatchQueue.main.async { + var latest = positions + .filter { $0.latest == true } + .sorted { $0.nodePosition?.num ?? 0 > $1.nodePosition?.num ?? -1 } if showRouteLines { // Remove all existing PolyLine Overlays @@ -118,19 +121,28 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.removeOverlay(overlay) } } - let nodePositions = positions// positions.filter { $0.time! >= Calendar.current.startOfDay(for: Date()) } - let lineCoords = nodePositions.map ({ - (position) -> CLLocationCoordinate2D in - return position.nodeCoordinate! - }) - let polyline = MKPolyline(coordinates: lineCoords, count: nodePositions.count) - mapView.addOverlay(polyline) + var lineIndex = 0 + for position in latest { + + let nodePositions = positions.filter { $0.time! >= Calendar.current.startOfDay(for: Date()) && $0.nodePosition?.num ?? 0 == position.nodePosition?.num ?? -1 } + let lineCoords = nodePositions.map ({ + (position) -> CLLocationCoordinate2D in + return position.nodeCoordinate! + }) + let polyline = MKPolyline(coordinates: lineCoords, count: nodePositions.count) + polyline.title = "\(String(position.nodePosition?.num ?? 0))-\(String(lineIndex))" + mapView.addOverlay(polyline) + lineIndex += 1 + // There are 10 colors for lines, start over if we are at index 11 + if lineIndex > 9 { + lineIndex = 0 + } + } } let annotationCount = waypoints.count + positions.count if annotationCount != mapView.annotations.count { mapView.removeAnnotations(mapView.annotations) - let latest = positions.filter { $0.latest == true } mapView.addAnnotations(waypoints) mapView.setUserTrackingMode(userTrackingMode, animated: true) @@ -161,7 +173,6 @@ struct MapViewSwiftUI: UIViewRepresentable { var parent: MapViewSwiftUI var longPressRecognizer = UILongPressGestureRecognizer() - //var overlays: [Overlay] = [] init(_ parent: MapViewSwiftUI) { self.parent = parent @@ -171,7 +182,6 @@ struct MapViewSwiftUI: UIViewRepresentable { self.longPressRecognizer.cancelsTouchesInView = true self.longPressRecognizer.delegate = self self.parent.mapView.addGestureRecognizer(longPressRecognizer) - //self.overlays = [] } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { @@ -340,8 +350,10 @@ struct MapViewSwiftUI: UIViewRepresentable { } else { if let routePolyline = overlay as? MKPolyline { + let titleString = routePolyline.title ?? "None-0" + let index = Int(titleString.components(separatedBy: "-").last ?? "0") let renderer = MKPolylineRenderer(polyline: routePolyline) - renderer.strokeColor = UIColor.systemIndigo + renderer.strokeColor = parent.colors[index ?? 0] renderer.lineWidth = 5 return renderer } diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index 590bbace..42545bd1 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -32,6 +32,7 @@ struct NodeMap: View { @AppStorage("meshMapType") private var meshMapType = "hybridFlyover" @AppStorage("meshMapUserTrackingMode") private var meshMapUserTrackingMode = 0 @AppStorage("meshMapShowNodeHistory") private var meshMapShowNodeHistory = false + @AppStorage("meshMapShowRouteLines") private var meshMapShowRouteLines = false @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "time", ascending: true)], predicate: NSPredicate(format: "time >= %@ && nodePosition != nil", Calendar.current.startOfDay(for: Date()) as NSDate), animation: .none) @@ -76,7 +77,7 @@ struct NodeMap: View { waypoints: Array(waypoints), mapViewType: mapType, userTrackingMode: userTrackingMode, - showRouteLines: false, + showRouteLines: meshMapShowRouteLines, showNodeHistory: meshMapShowNodeHistory, customMapOverlay: self.customMapOverlay )