Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store current road details in map view controller #297

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ import MapboxCoreNavigation
class ArrowFillPolyline: MGLPolylineFeature {}
class ArrowStrokePolyline: ArrowFillPolyline {}

struct Road {
let name: String?
let classification: RoadClassification
}

enum RoadClassification: String {
case motorway = "motorway"
case motorwayLink = "motorway_link"
case trunk = "trunk"
case primary = "primary"
case secondary = "secondary"
case tertiary = "tertiary"
case link = "link"
case street = "street"
case limitedStreet = "street_limited"
case pedestrian = "pedestrian"
case construction = "construction"
case track = "track"
case service = "service"
case ferry = "ferry"
case path = "path"
case majorRail = "major_rail"
case minorRail = "minor_rail"
case aerialway = "aerialway"
case golf = "golf"
}

class RouteMapViewController: UIViewController {
@IBOutlet weak var mapView: NavigationMapView!
Expand Down Expand Up @@ -52,6 +78,13 @@ class RouteMapViewController: UIViewController {

var arrowCurrentStep: RouteStep?
var isInOverviewMode = false

var currentRoad: Road? {
didSet {
wayNameLabel.text = currentRoad?.name
wayNameView.isHidden = currentRoad?.name == nil || currentRoad!.name!.isEmpty
}
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -309,7 +342,7 @@ extension RouteMapViewController: NavigationMapViewDelegate {
let userPuck = mapView.convert(closestCoordinate, toPointTo: mapView)
let features = mapView.visibleFeatures(at: userPuck, styleLayerIdentifiers: Set([roadLabelLayerIdentifier]))
var smallestLabelDistance = Double.infinity
var currentName: String?
var closestRoad: Road?

for feature in features {
var allLines: [MGLPolyline] = []
Expand All @@ -336,23 +369,19 @@ extension RouteMapViewController: NavigationMapViewDelegate {
if minDistanceBetweenPoints < smallestLabelDistance {
smallestLabelDistance = minDistanceBetweenPoints

if let line = feature as? MGLPolylineFeature, let name = line.attribute(forKey: "name") as? String {
currentName = name
} else if let line = feature as? MGLMultiPolylineFeature, let name = line.attribute(forKey: "name") as? String {
currentName = name
if let line = (feature as? MGLPolylineFeature ?? feature as? MGLMultiPolylineFeature) as? MGLFeature {
let name = line.attribute(forKey: "name") as? String
let rawClassification = line.attribute(forKey: "class") as! String
let classification = RoadClassification(rawValue: rawClassification) ?? .street
closestRoad = Road(name: name, classification: classification)
} else {
currentName = nil
closestRoad = nil
}
}
}
}

if smallestLabelDistance < 5 && currentName != nil {
wayNameLabel.text = currentName
wayNameView.isHidden = false
} else {
wayNameView.isHidden = true
}
currentRoad = smallestLabelDistance < 5 ? closestRoad : nil
}


Expand Down