-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visible annotations to the map at intersecting roads along the ac…
…tive route as well as at maneuver points.
- Loading branch information
Showing
20 changed files
with
751 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import UIKit | ||
import Turf | ||
import MapboxDirections | ||
import MapboxCoreNavigation | ||
import MapboxNavigation | ||
import MapboxCoreMaps | ||
import MapboxMaps | ||
|
||
// MARK: - Visible annotations on the map about the current drive | ||
|
||
extension NavigationMapView { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import MapboxCoreNavigation | ||
|
||
extension ElectronicHorizon.Edge { | ||
var mpp: [ElectronicHorizon.Edge]? { | ||
|
||
guard level == 0 else { return nil } | ||
|
||
var mostProbablePath = [self] | ||
|
||
for child in outletEdges { | ||
if let childMPP = child.mpp { | ||
mostProbablePath.append(contentsOf: childMPP) | ||
} | ||
} | ||
|
||
return mostProbablePath | ||
} | ||
|
||
func edgeNames(roadGraph: RoadGraph) -> [String] { | ||
guard let metadata = roadGraph.edgeMetadata(edgeIdentifier: identifier) else { | ||
return [] | ||
} | ||
let names = metadata.names.map { name -> String in | ||
switch name { | ||
case .name(let name): | ||
return name | ||
case .code(let code): | ||
return "(\(code))" | ||
} | ||
} | ||
|
||
// If the road is unnamed, fall back to the road class. | ||
if names.isEmpty { | ||
return ["\(metadata.mapboxStreetsRoadClass.rawValue)"] | ||
} | ||
return names | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
Sources/MapboxNavigation/NavigationMapView+IntersectionAnnotations.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import CoreLocation | ||
import UIKit | ||
import MapboxDirections | ||
import MapboxCoreNavigation | ||
import Turf | ||
import MapboxMaps | ||
|
||
extension NavigationMapView { | ||
|
||
struct EdgeIntersection { | ||
var root: ElectronicHorizon.Edge | ||
var branch: ElectronicHorizon.Edge | ||
var rootMetadata: ElectronicHorizon.Edge.Metadata | ||
var rootShape: LineString | ||
var branchMetadata: ElectronicHorizon.Edge.Metadata | ||
var branchShape: LineString | ||
|
||
var coordinate: CLLocationCoordinate2D? { | ||
rootShape.coordinates.first | ||
} | ||
|
||
var annotationPoint: CLLocationCoordinate2D? { | ||
guard let length = branchShape.distance() else { return nil } | ||
let targetDistance = min(length / 2, Double.random(in: 15...30)) | ||
guard let annotationPoint = branchShape.coordinateFromStart(distance: targetDistance) else { return nil } | ||
return annotationPoint | ||
} | ||
|
||
var wayName: String? { | ||
guard let roadName = rootMetadata.names.first else { return nil } | ||
|
||
switch roadName { | ||
case .name(let name): | ||
return name | ||
case .code(let code): | ||
return "(\(code))" | ||
} | ||
} | ||
var intersectingWayName: String? { | ||
guard let roadName = branchMetadata.names.first else { return nil } | ||
|
||
switch roadName { | ||
case .name(let name): | ||
return name | ||
case .code(let code): | ||
return "(\(code))" | ||
} | ||
} | ||
|
||
var incidentAngle: CLLocationDegrees { | ||
return (branchMetadata.heading - rootMetadata.heading).wrap(min: 0, max: 360) | ||
} | ||
|
||
var description: String { | ||
return "EdgeIntersection: root: \(wayName ?? "") intersection: \(intersectingWayName ?? "") coordinate: \(String(describing: coordinate))" | ||
} | ||
} | ||
|
||
enum AnnotationTailPosition: Int { | ||
case left | ||
case right | ||
case center | ||
} | ||
|
||
class AnnotationCacheEntry: Equatable, Hashable { | ||
var wayname: String | ||
var coordinate: CLLocationCoordinate2D | ||
var intersection: EdgeIntersection? | ||
var feature: Feature | ||
var lastAccessTime: Date | ||
|
||
init(coordinate: CLLocationCoordinate2D, wayname: String, intersection: EdgeIntersection? = nil, feature: Feature) { | ||
self.wayname = wayname | ||
self.coordinate = coordinate | ||
self.intersection = intersection | ||
self.feature = feature | ||
self.lastAccessTime = Date() | ||
} | ||
|
||
static func == (lhs: AnnotationCacheEntry, rhs: AnnotationCacheEntry) -> Bool { | ||
return lhs.wayname == rhs.wayname | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(wayname.hashValue) | ||
} | ||
} | ||
|
||
class AnnotationCache { | ||
private let maxEntryAge = TimeInterval(30) | ||
var entries = Set<AnnotationCacheEntry>() | ||
var cachePruningTimer: Timer? | ||
|
||
init() { | ||
// periodically prune the cache to remove entries that have been passed already | ||
cachePruningTimer = Timer.scheduledTimer(withTimeInterval: 15, repeats: true, block: { [weak self] _ in | ||
self?.prune() | ||
}) | ||
} | ||
|
||
deinit { | ||
cachePruningTimer?.invalidate() | ||
cachePruningTimer = nil | ||
} | ||
|
||
func setValue(feature: Feature, coordinate: CLLocationCoordinate2D, intersection: EdgeIntersection?, for wayname: String) { | ||
entries.insert(AnnotationCacheEntry(coordinate: coordinate, wayname: wayname, intersection: intersection, feature: feature)) | ||
} | ||
|
||
func value(for wayname: String) -> AnnotationCacheEntry? { | ||
let matchingEntry = entries.first { entry -> Bool in | ||
entry.wayname == wayname | ||
} | ||
|
||
if let matchingEntry = matchingEntry { | ||
// update the timestamp used for pruning the cache | ||
matchingEntry.lastAccessTime = Date() | ||
} | ||
|
||
return matchingEntry | ||
} | ||
|
||
private func prune() { | ||
let now = Date() | ||
|
||
entries.filter { now.timeIntervalSince($0.lastAccessTime) > maxEntryAge }.forEach { remove($0) } | ||
} | ||
|
||
public func remove(_ entry: AnnotationCacheEntry) { | ||
entries.remove(entry) | ||
} | ||
} | ||
} |
Oops, something went wrong.