Meshtastic-Apple/Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift
Copilot 93061a824c Implement map legend overlay (#1653)
* Add map legend feature (issue #924)

Implement a map legend overlay accessible from both the mesh map and node
detail map views. The legend explains all visual map elements including:
- Online/offline node markers with pulsing animation
- Detection sensor nodes
- Waypoints
- Position precision circles
- Position history points and heading arrows
- Route start/end markers and route lines
- Convex hull mesh coverage outline

A new "map" button is added to the floating control buttons on both map
views, opening the legend as a sheet.

Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/23f75e1e-549b-46a1-84c9-fb0a6375dcd9

Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>

* Improve legend descriptions for online/offline nodes

Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/23f75e1e-549b-46a1-84c9-fb0a6375dcd9

Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>

* map button glass and cleanup

* Update Meshtastic/Views/Nodes/Helpers/Map/MapLegend.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update incorect online timeframe

* Update Meshtastic/Views/Nodes/MeshMap.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* translation file

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>
Co-authored-by: Garth Vander Houwen <garthvh@yahoo.com>
Co-authored-by: Garth Vander Houwen <garth@meshtastic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-16 06:06:55 +00:00

277 lines
8.8 KiB
Swift

//
// NodeMapSwiftUI.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 9/11/23.
//
import SwiftUI
import CoreLocation
import MapKit
struct NodeMapContentSignature: Equatable {
// Used to decide if NodeMapContent needs to be reevaluated.
// Only include fields that are used within NodeMapContent (or approximations like positionCount and lastPositionTime).
let nodeNum: Int64
let positionCount: Int
let lastPositionTime: Date?
let showNodeHistory: Bool
let showRouteLines: Bool
let showConvexHull: Bool
let favorite: Bool
}
private struct NodeMapContentEquatableWrapper<Content: View>: View, Equatable {
// Prevent slow, needless recomputation of NodeMapContent if the NodeMapContentSignature hasn't changed.
let signature: NodeMapContentSignature
@ViewBuilder let content: () -> Content
static func == (lhs: NodeMapContentEquatableWrapper<Content>, rhs: NodeMapContentEquatableWrapper<Content>) -> Bool { lhs.signature == rhs.signature }
var body: some View { content() }
}
struct NodeMapSwiftUI: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var accessoryManager: AccessoryManager
/// Parameters
@ObservedObject var node: NodeInfoEntity
@State var showUserLocation: Bool = false
@State var positions: [PositionEntity] = []
/// Map State User Defaults
@AppStorage("meshMapShowNodeHistory") private var showNodeHistory = false
@AppStorage("meshMapShowRouteLines") private var showRouteLines = false
@AppStorage("enableMapConvexHull") private var showConvexHull = false
@AppStorage("enableMapTraffic") private var showTraffic: Bool = false
@AppStorage("enableMapPointsOfInterest") private var showPointsOfInterest: Bool = false
@AppStorage("mapLayer") private var selectedMapLayer: MapLayer = .hybrid
// Map Configuration
@Namespace var mapScope
@State var mapStyle: MapStyle = MapStyle.hybrid(elevation: .flat, pointsOfInterest: .all, showsTraffic: true)
@State var position = MapCameraPosition.automatic
@State var distance = 10000.0
@State var scene: MKLookAroundScene?
@State var isLookingAround = false
@State var isShowingAltitude = false
@State var isEditingSettings = false
@State var isShowingLegend = false
@State var isMeshMap = false
@State var enabledOverlayConfigs: Set<UUID> = Set()
@State private var mapRegion = MKCoordinateRegion.init()
@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: false)],
predicate: NSPredicate(
format: "expire == nil || expire >= %@", Date() as NSDate
), animation: .none)
private var waypoints: FetchedResults<WaypointEntity>
var body: some View {
if node.hasPositions {
mapWithNavigation
} else {
ContentUnavailableView("No Positions", systemImage: "mappin.slash")
}
}
private var mapWithNavigation: some View {
ZStack {
MapReader { _ in
configuredMap
}
}
.navigationBarTitle(String((node.user?.shortName ?? "Unknown".localized) + (" \(node.positions?.count ?? 0) points")), displayMode: .inline)
.navigationBarItems(trailing:
ZStack {
ConnectedDevice(
deviceConnected: accessoryManager.isConnected,
name: accessoryManager.activeConnection?.device.shortName ?? "?")
})
}
private var configuredMap: some View {
baseMap
.overlay(alignment: .bottom) {
lookAroundView
}
.overlay(alignment: .bottom) {
altitudeView
}
.sheet(isPresented: $isEditingSettings) {
MapSettingsForm(traffic: $showTraffic, pointsOfInterest: $showPointsOfInterest, mapLayer: $selectedMapLayer, meshMap: $isMeshMap, enabledOverlayConfigs: $enabledOverlayConfigs)
}
.sheet(isPresented: $isShowingLegend) {
MapLegend(isMeshMap: false)
.presentationDetents([.medium, .large])
.presentationContentInteraction(.scrolls)
.presentationDragIndicator(.visible)
.presentationBackgroundInteraction(.enabled(upThrough: .medium))
}
.onChange(of: selectedMapLayer) { _, newMapLayer in
updateMapStyle(for: newMapLayer)
}
.onChange(of: node) {
handleNodeChange()
}
.onAppear {
handleAppear()
}
.safeAreaInset(edge: .bottom, alignment: .trailing) {
controlButtons
}
.onDisappear {
UIApplication.shared.isIdleTimerDisabled = false
}
}
private var mapContentSignature: NodeMapContentSignature {
let positionCount = node.positions?.count ?? 0
let lastPositionTime = (node.positions?.lastObject as? PositionEntity)?.time
return NodeMapContentSignature(nodeNum: node.num, positionCount: positionCount, lastPositionTime: lastPositionTime, showNodeHistory: showNodeHistory, showRouteLines: showRouteLines, showConvexHull: showConvexHull, favorite: node.favorite)
}
private var baseMap: some View {
NodeMapContentEquatableWrapper(signature: mapContentSignature) {
Map(position: $position, bounds: MapCameraBounds(minimumDistance: 0, maximumDistance: .infinity), scope: mapScope) {
NodeMapContent(node: node)
}
}
.mapScope(mapScope)
.mapStyle(mapStyle)
.mapControls {
MapScaleView(scope: mapScope)
.mapControlVisibility(.visible)
if showUserLocation {
MapUserLocationButton(scope: mapScope)
.mapControlVisibility(.visible)
}
MapPitchToggle(scope: mapScope)
.mapControlVisibility(.visible)
MapCompass(scope: mapScope)
.mapControlVisibility(.visible)
}
.controlSize(.regular)
.transaction { $0.animation = nil }
}
private var lookAroundView: some View {
Group {
if scene != nil && isLookingAround {
LookAroundPreview(initialScene: scene)
.frame(height: UIDevice.current.userInterfaceIdiom == .phone ? 250 : 400)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 20)
}
}
}
private var altitudeView: some View {
Group {
if !isLookingAround && isShowingAltitude {
PositionAltitudeChart(node: node)
.frame(height: UIDevice.current.userInterfaceIdiom == .phone ? 250 : 400)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 20)
}
}
}
private var controlButtons: some View {
HStack {
Button(action: {
withAnimation {
isShowingLegend = !isShowingLegend
}
}) {
Image(systemName: isShowingLegend ? "map.fill" : "map")
}
.accessibilityLabel(isShowingLegend ? Text("Hide legend") : Text("Show legend"))
.accessibilityHint(Text("Toggles the map legend"))
.glassButtonStyle()
Button(action: {
withAnimation {
isEditingSettings = !isEditingSettings
}
}) {
Image(systemName: isEditingSettings ? "info.circle.fill" : "info.circle")
}
.glassButtonStyle()
if scene != nil {
Button(action: {
if isShowingAltitude {
isShowingAltitude = false
}
isLookingAround = !isLookingAround
}) {
Image(systemName: isLookingAround ? "binoculars.fill" : "binoculars")
}
.glassButtonStyle()
}
if node.positions?.count ?? 0 > 1 {
Button(action: {
if isLookingAround {
isLookingAround = false
}
isShowingAltitude = !isShowingAltitude
}) {
Image(systemName: isShowingAltitude ? "mountain.2.fill" : "mountain.2")
}
.glassButtonStyle()
}
}
.controlSize(.regular)
.padding(5)
}
private func updateMapStyle(for layer: MapLayer) {
UserDefaults.mapLayer = layer
switch layer {
case .standard:
mapStyle = MapStyle.standard(elevation: .flat, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .hybrid:
mapStyle = MapStyle.hybrid(elevation: .flat, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .satellite:
mapStyle = MapStyle.imagery(elevation: .flat)
case .offline:
break
}
}
private func handleNodeChange() {
isLookingAround = false
isShowingAltitude = false
let newMostRecent = node.positions?.lastObject as? PositionEntity
if node.positions?.count ?? 0 > 1 {
position = .automatic
} else if let mrCoord = newMostRecent?.coordinate {
position = .camera(MapCamera(centerCoordinate: mrCoord, distance: distance, heading: 0, pitch: 0))
}
if let newMostRecent {
Task {
scene = try? await fetchScene(for: newMostRecent.coordinate)
}
}
}
private func handleAppear() {
UIApplication.shared.isIdleTimerDisabled = true
updateMapStyle(for: selectedMapLayer)
let mostRecent = node.positions?.lastObject as? PositionEntity
if node.positions?.count ?? 0 > 1 {
position = .automatic
} else if let mrCoord = mostRecent?.coordinate {
position = .camera(MapCamera(centerCoordinate: mrCoord, distance: distance, heading: 0, pitch: 0))
}
if scene == nil, let mrCoord = mostRecent?.coordinate {
Task {
scene = try? await fetchScene(for: mrCoord)
}
}
}
/// Get the look around scene
private func fetchScene(for coordinate: CLLocationCoordinate2D) async throws -> MKLookAroundScene? {
let lookAroundScene = MKLookAroundSceneRequest(coordinate: coordinate)
return try await lookAroundScene.scene
}
}