Skip to content

Commit

Permalink
Determine current road classification
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Jun 20, 2017
1 parent 8c9a7d5 commit 612d06c
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ import SDWebImage
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 All @@ -35,17 +61,6 @@ class RouteMapViewController: UIViewController {
return parent.pendingCamera
}
weak var delegate: RouteMapViewControllerDelegate?

var currentRoadName: String? {
didSet {
if let currentRoadName = currentRoadName {
wayNameLabel.text = currentRoadName
wayNameView.isHidden = false
} else {
wayNameView.isHidden = true
}
}
}

weak var routeController: RouteController!

Expand All @@ -63,6 +78,13 @@ class RouteMapViewController: UIViewController {
guard let parent = parent as? NavigationViewController else { return false }
return parent.simulatesLocationUpdates
}

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 @@ -382,7 +404,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 @@ -409,18 +431,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
}
}
}
}

currentRoadName = smallestLabelDistance < 5 ? currentName : nil
currentRoad = smallestLabelDistance < 5 ? closestRoad : nil
}


Expand Down

0 comments on commit 612d06c

Please sign in to comment.