|
| 1 | +import CoreLocation |
| 2 | +import UIKit |
| 3 | +import MapboxDirections |
| 4 | +import MapboxCoreNavigation |
| 5 | +import Turf |
| 6 | +import MapboxMaps |
| 7 | + |
| 8 | +extension NavigationMapView { |
| 9 | + |
| 10 | + struct EdgeIntersection { |
| 11 | + var root: RoadGraph.Edge |
| 12 | + var branch: RoadGraph.Edge |
| 13 | + var rootMetadata: RoadGraph.Edge.Metadata |
| 14 | + var rootShape: LineString |
| 15 | + var branchMetadata: RoadGraph.Edge.Metadata |
| 16 | + var branchShape: LineString |
| 17 | + |
| 18 | + var coordinate: CLLocationCoordinate2D? { |
| 19 | + rootShape.coordinates.first |
| 20 | + } |
| 21 | + |
| 22 | + var annotationPoint: CLLocationCoordinate2D? { |
| 23 | + guard let length = branchShape.distance() else { return nil } |
| 24 | + let targetDistance = min(length / 2, Double.random(in: 15...30)) |
| 25 | + guard let annotationPoint = branchShape.coordinateFromStart(distance: targetDistance) else { return nil } |
| 26 | + return annotationPoint |
| 27 | + } |
| 28 | + |
| 29 | + var wayName: String? { |
| 30 | + guard let roadName = rootMetadata.names.first else { return nil } |
| 31 | + |
| 32 | + switch roadName { |
| 33 | + case .name(let name): |
| 34 | + return name |
| 35 | + case .code(let code): |
| 36 | + return "(\(code))" |
| 37 | + } |
| 38 | + } |
| 39 | + var intersectingWayName: String? { |
| 40 | + guard let roadName = branchMetadata.names.first else { return nil } |
| 41 | + |
| 42 | + switch roadName { |
| 43 | + case .name(let name): |
| 44 | + return name |
| 45 | + case .code(let code): |
| 46 | + return "(\(code))" |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + var incidentAngle: CLLocationDegrees { |
| 51 | + return (branchMetadata.heading - rootMetadata.heading).wrap(min: 0, max: 360) |
| 52 | + } |
| 53 | + |
| 54 | + var description: String { |
| 55 | + return "EdgeIntersection: root: \(wayName ?? "") intersection: \(intersectingWayName ?? "") coordinate: \(String(describing: coordinate))" |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + enum AnnotationTailPosition: Int { |
| 60 | + case left |
| 61 | + case right |
| 62 | + case center |
| 63 | + } |
| 64 | + |
| 65 | + class AnnotationCacheEntry: Equatable, Hashable { |
| 66 | + var wayname: String |
| 67 | + var coordinate: CLLocationCoordinate2D |
| 68 | + var intersection: EdgeIntersection? |
| 69 | + var feature: Feature |
| 70 | + var lastAccessTime: Date |
| 71 | + |
| 72 | + init(coordinate: CLLocationCoordinate2D, wayname: String, intersection: EdgeIntersection? = nil, feature: Feature) { |
| 73 | + self.wayname = wayname |
| 74 | + self.coordinate = coordinate |
| 75 | + self.intersection = intersection |
| 76 | + self.feature = feature |
| 77 | + self.lastAccessTime = Date() |
| 78 | + } |
| 79 | + |
| 80 | + static func == (lhs: AnnotationCacheEntry, rhs: AnnotationCacheEntry) -> Bool { |
| 81 | + return lhs.wayname == rhs.wayname |
| 82 | + } |
| 83 | + |
| 84 | + func hash(into hasher: inout Hasher) { |
| 85 | + hasher.combine(wayname.hashValue) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + class AnnotationCache { |
| 90 | + private let maxEntryAge = TimeInterval(30) |
| 91 | + var entries = Set<AnnotationCacheEntry>() |
| 92 | + var cachePruningTimer: Timer? |
| 93 | + |
| 94 | + init() { |
| 95 | + // periodically prune the cache to remove entries that have been passed already |
| 96 | + cachePruningTimer = Timer.scheduledTimer(withTimeInterval: 15, repeats: true, block: { [weak self] _ in |
| 97 | + self?.prune() |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + deinit { |
| 102 | + cachePruningTimer?.invalidate() |
| 103 | + cachePruningTimer = nil |
| 104 | + } |
| 105 | + |
| 106 | + func setValue(feature: Feature, coordinate: CLLocationCoordinate2D, intersection: EdgeIntersection?, for wayname: String) { |
| 107 | + entries.insert(AnnotationCacheEntry(coordinate: coordinate, wayname: wayname, intersection: intersection, feature: feature)) |
| 108 | + } |
| 109 | + |
| 110 | + func value(for wayname: String) -> AnnotationCacheEntry? { |
| 111 | + let matchingEntry = entries.first { entry -> Bool in |
| 112 | + entry.wayname == wayname |
| 113 | + } |
| 114 | + |
| 115 | + if let matchingEntry = matchingEntry { |
| 116 | + // update the timestamp used for pruning the cache |
| 117 | + matchingEntry.lastAccessTime = Date() |
| 118 | + } |
| 119 | + |
| 120 | + return matchingEntry |
| 121 | + } |
| 122 | + |
| 123 | + private func prune() { |
| 124 | + let now = Date() |
| 125 | + |
| 126 | + entries.filter { now.timeIntervalSince($0.lastAccessTime) > maxEntryAge }.forEach { remove($0) } |
| 127 | + } |
| 128 | + |
| 129 | + public func remove(_ entry: AnnotationCacheEntry) { |
| 130 | + entries.remove(entry) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments