Skip to content

Commit

Permalink
fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Sudekum committed Apr 20, 2017
1 parent b298170 commit 04cba87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
29 changes: 16 additions & 13 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class RouteMapViewController: UIViewController, PulleyPrimaryContentControllerDe
var shieldAPIDataTask: URLSessionDataTask?
var shieldImageDownloadToken: SDWebImageDownloadToken?
var arrowCurrentStep: RouteStep?
var currentWayNameCounter = 0

var simulatesLocationUpdates: Bool {
guard let parent = parent as? NavigationViewController else { return false }
Expand Down Expand Up @@ -264,7 +263,7 @@ extension RouteMapViewController: NavigationMapViewDelegate {
let style = mapView.style,
recenterButton.isHidden {

let streetsLanguages = ["zh", "ru", "fr", "es", "en"]
let streetsLanguages = ["zh", "ru", "fr", "es", "en", "de"]
let roadLabelLayerIdentifier = "roadLabelLayer"

let streetsSources = style.sources.flatMap {
Expand All @@ -288,29 +287,34 @@ extension RouteMapViewController: NavigationMapViewDelegate {
style.addLayer(streetLabelLayer)
}

if let userPuck = mapView.view(for: userLocation) {
if let userPuck = mapView.view(for: userLocation),
let routeWayNames = routeController.routeProgress.currentLegProgress.currentStep.names {
let features = mapView.visibleFeatures(in: userPuck.frame, styleLayerIdentifiers: Set([roadLabelLayerIdentifier]))

for feature in features {

var key = "name"
if let language = Locale.preferredLanguages.first!.components(separatedBy: "-").first,
streetsLanguages.contains(language) || language == "zh-Hans" {
if let languages = Locale.preferredLanguages.first,
let language = languages.components(separatedBy: "-").first,
streetsLanguages.contains(language) || languages == "zh-Hans" {
key += "_\(language)"
}

if let name = feature.attribute(forKey: key) as? String {
if name != wayNameLabel.text {
currentWayNameCounter += 1

let filteredStreetNames = routeWayNames.filter {
return name.distanceFrom(string: $0) <= 6
}
print(currentWayNameCounter)
if currentWayNameCounter >= 10 {

if !filteredStreetNames.isEmpty {
wayNameLabel.text = name
currentWayNameCounter = 0
}
wayNameLabel.sizeToFit()
wayNameLabel.isHidden = false
} else if let routeWayName = routeWayNames.first {
wayNameLabel.text = routeWayName
}

wayNameLabel.sizeToFit()
wayNameLabel.isHidden = false
}
}
}
Expand Down Expand Up @@ -401,7 +405,6 @@ extension RouteMapViewController: MGLMapViewDelegate {
if mode != .followWithCourse {
recenterButton.isHidden = false
wayNameLabel.isHidden = true
currentWayNameCounter = 0
startResetTrackingModeTimer()
} else {
recenterButton.isHidden = true
Expand Down
38 changes: 38 additions & 0 deletions MapboxNavigation/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,41 @@ extension String {
])
}
}

// From https://gist.github.com/joncardasis/7f09d1a55f3278d8ee5080e47653caff
extension String{
private func min(numbers: Int...) -> Int {
return numbers.reduce(numbers[0], {$0 < $1 ? $0 : $1})
}

func distanceFrom(string: String) -> Int{
let x = Array(self.utf16) //convert to a unicode 16 format for comparison
let y = Array(string.utf16)

//Create the Levenshtein 2d matrix, which has an extra preceeding row and column
var matrix = Array(repeating: [Int](repeating: 0, count: y.count + 1), count: x.count + 1)

for i in 1...x.count{ //set rows (0,0 is already set from repeated value)
matrix[i][0] = i
}
for j in 1...y.count{ //set columns
matrix[0][j] = j
}

for row in 1...x.count{
for col in 1...y.count {
if(x[row-1] == y[col-1]){
matrix[row][col] = matrix[row-1][col-1] //Match
}
else{
matrix[row][col] = min(numbers:
matrix[row-1][col-1] + 1, //Subsitution
matrix[row-1][col] + 1, //Deletion
matrix[row][col-1] + 1 //Insertion
)
}
}
}
return matrix[x.count][y.count]
}
}

0 comments on commit 04cba87

Please sign in to comment.