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

Show current way name #155

Merged
merged 28 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ open class RouteController: NSObject {
*/
public var snapsUserLocationAnnotationToRoute = false

/**
If true, the way name the user is current traveling on will be displayed.

Note: the feature queries the underlying tile data and makes the assumption the current style includes the source `mapbox://mapbox.streets-v7`. All default Mapbox styles include this source. If you are using a custom source, make sure it is included when using this feature. Otherwise, it will be ignored.
*/
public var showCurrentWayNameLabel = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still need to be an option?


public var simulatesLocationUpdates: Bool = false {
didSet {
locationManager.delegate = simulatesLocationUpdates ? nil : self
Expand Down
4 changes: 4 additions & 0 deletions MapboxNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
C52D09CE1DEF5E5100BE3C5C /* route.json in Resources */ = {isa = PBXBuildFile; fileRef = C52D09CD1DEF5E5100BE3C5C /* route.json */; };
C52D09D31DEF636C00BE3C5C /* Fixture.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52D09D21DEF636C00BE3C5C /* Fixture.swift */; };
C53208AB1E81FFB900910266 /* NavigationMapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C53208AA1E81FFB900910266 /* NavigationMapView.swift */; };
C58159011EA6D02700FC6C3D /* MGLVectorSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58159001EA6D02700FC6C3D /* MGLVectorSource.swift */; };
C58D6BAD1DDCF2AE00387F53 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58D6BAC1DDCF2AE00387F53 /* Constants.swift */; };
C5ADFBD81DDCC7840011824B /* MapboxCoreNavigationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5ADFBD71DDCC7840011824B /* MapboxCoreNavigationTests.swift */; };
C5C94C1B1DDCD22B0097296A /* MapboxCoreNavigation.h in Headers */ = {isa = PBXBuildFile; fileRef = C5ADFBCC1DDCC7840011824B /* MapboxCoreNavigation.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -312,6 +313,7 @@
C52D09CD1DEF5E5100BE3C5C /* route.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = route.json; sourceTree = "<group>"; };
C52D09D21DEF636C00BE3C5C /* Fixture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fixture.swift; sourceTree = "<group>"; };
C53208AA1E81FFB900910266 /* NavigationMapView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationMapView.swift; sourceTree = "<group>"; };
C58159001EA6D02700FC6C3D /* MGLVectorSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MGLVectorSource.swift; sourceTree = "<group>"; };
C58D6BAC1DDCF2AE00387F53 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
C5ADFBC91DDCC7840011824B /* MapboxCoreNavigation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MapboxCoreNavigation.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C5ADFBCC1DDCC7840011824B /* MapboxCoreNavigation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MapboxCoreNavigation.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -563,6 +565,7 @@
35D825FA1E6A2DBE0088F83B /* MGLMapView+MGLNavigationAdditions.m */,
351BEBEB1E5BCC63006FE110 /* String.swift */,
351BEBF01E5BCC63006FE110 /* UIView.swift */,
C58159001EA6D02700FC6C3D /* MGLVectorSource.swift */,
);
name = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1071,6 +1074,7 @@
35EC316D1EA64057004280F5 /* SimulatedRoute.swift in Sources */,
C53208AB1E81FFB900910266 /* NavigationMapView.swift in Sources */,
351BEBF61E5BCC63006FE110 /* RouteMapViewController.swift in Sources */,
C58159011EA6D02700FC6C3D /* MGLVectorSource.swift in Sources */,
351BEBF51E5BCC63006FE110 /* RouteManeuverViewController.swift in Sources */,
351BEC011E5BCC63006FE110 /* TurnArrowView.swift in Sources */,
351BEBFA1E5BCC63006FE110 /* RouteTableViewController.swift in Sources */,
Expand Down
19 changes: 19 additions & 0 deletions MapboxNavigation/MGLVectorSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// MGLVectorSource.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Drop default header comments

// MapboxNavigation
//
// Created by Bobby Sudekum on 4/18/17.
// Copyright © 2017 Mapbox. All rights reserved.
//

import Foundation
import Mapbox

extension MGLVectorSource {
var isMapboxStreets: Bool {
guard let configurationURL = configurationURL else {
return false
}
return configurationURL.scheme == "mapbox" && configurationURL.host!.components(separatedBy: ",").contains("mapbox.mapbox-streets-v7")
}
}
15 changes: 15 additions & 0 deletions MapboxNavigation/Resources/Navigation.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
<action selector="recenter:" destination="3z2-H5-unA" eventType="touchUpInside" id="s72-hB-PTq"/>
</connections>
</button>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Street Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bcm-ca-nGv" customClass="MBStyleLabel">
<rect key="frame" x="146" y="213.5" width="83" height="18"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="inspectableTextStyle">
<integer key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</label>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="p9E-v0-Rt6" userLabel="Maneuver View">
<rect key="frame" x="0.0" y="0.0" width="375" height="100"/>
<constraints>
Expand All @@ -101,18 +113,21 @@
<constraints>
<constraint firstItem="nNr-30-cGD" firstAttribute="top" secondItem="Gvo-VD-DXF" secondAttribute="top" id="0tN-KV-QpL"/>
<constraint firstItem="nNr-30-cGD" firstAttribute="leading" secondItem="Gvo-VD-DXF" secondAttribute="leading" id="4Si-h7-lL5"/>
<constraint firstItem="bcm-ca-nGv" firstAttribute="centerX" secondItem="nNr-30-cGD" secondAttribute="centerX" id="Bgv-8D-8cu"/>
<constraint firstItem="p9E-v0-Rt6" firstAttribute="leading" secondItem="Gvo-VD-DXF" secondAttribute="leading" id="DvR-Su-3GN"/>
<constraint firstItem="p9E-v0-Rt6" firstAttribute="top" secondItem="Gvo-VD-DXF" secondAttribute="top" id="K6m-Le-IeP"/>
<constraint firstAttribute="trailing" secondItem="p9E-v0-Rt6" secondAttribute="trailing" id="UtJ-Ea-CLv"/>
<constraint firstAttribute="bottom" secondItem="nNr-30-cGD" secondAttribute="bottom" id="Wir-eN-QUd"/>
<constraint firstAttribute="trailing" secondItem="nNr-30-cGD" secondAttribute="trailing" id="gPI-GC-2rk"/>
<constraint firstItem="fVn-1d-beh" firstAttribute="top" secondItem="5HY-QD-dBq" secondAttribute="bottom" constant="100" id="leu-SU-9Qj"/>
<constraint firstItem="5HY-QD-dBq" firstAttribute="centerX" secondItem="Gvo-VD-DXF" secondAttribute="centerX" id="wDo-xG-s8o"/>
<constraint firstItem="fVn-1d-beh" firstAttribute="top" secondItem="bcm-ca-nGv" secondAttribute="bottom" constant="92" id="ww7-U1-39f"/>
</constraints>
</view>
<connections>
<outlet property="mapView" destination="nNr-30-cGD" id="Znv-B1-wdj"/>
<outlet property="recenterButton" destination="5HY-QD-dBq" id="6ev-zD-MJ9"/>
<outlet property="wayNameLabel" destination="bcm-ca-nGv" id="8Sy-Pk-fQZ"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="VoA-f6-EWh" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down
55 changes: 55 additions & 0 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ArrowStrokePolyline: ArrowFillPolyline {}
class RouteMapViewController: UIViewController, PulleyPrimaryContentControllerDelegate {
@IBOutlet weak var mapView: NavigationMapView!
@IBOutlet weak var recenterButton: UIButton!
@IBOutlet weak var wayNameLabel: StyleLabel!

var routePageViewController: RoutePageViewController!
var routeTableViewController: RouteTableViewController!
Expand Down Expand Up @@ -40,6 +41,9 @@ class RouteMapViewController: UIViewController, PulleyPrimaryContentControllerDe
var shieldImageDownloadToken: SDWebImageDownloadToken?
var arrowCurrentStep: RouteStep?

let streetsLanguages = ["zh", "ru", "fr", "es", "en"]
Copy link
Contributor

@1ec5 1ec5 Apr 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this documentation, the Mapbox Streets v7 source’s road_label layer has a name_ru property. There’s also an undocumented name_zh-Hans property that should be documented.

let RoadLabelLayerIdentifier = "roadLabelLayer"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is only used in one method, and there won’t ever be a need for another class to know about it. Make this a local variable instead.


var simulatesLocationUpdates: Bool {
guard let parent = parent as? NavigationViewController else { return false }
return parent.simulatesLocationUpdates
Expand All @@ -54,6 +58,10 @@ class RouteMapViewController: UIViewController, PulleyPrimaryContentControllerDe
mapView.tintColor = NavigationUI.shared.tintColor
recenterButton.tintColor = NavigationUI.shared.tintColor
recenterButton.applyDefaultCornerRadiusShadow(cornerRadius: 22)
wayNameLabel.applyDefaultCornerRadiusShadow()
wayNameLabel.layer.masksToBounds = true
wayNameLabel.insets = UIEdgeInsetsMake(1, 4, 1, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the Swift-style UIEdgeInsets(top:left:bottom:right:) instead of the C-style UIEdgeInsetsMake(_:_:_:_:).


}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -141,6 +149,7 @@ class RouteMapViewController: UIViewController, PulleyPrimaryContentControllerDe
mapView.addArrow(routeController.routeProgress)
mapView.showRoute(route)
mapView.userTrackingMode = .followWithCourse
wayNameLabel.isHidden = true
}

func notifyAlertLevelDidChange(routeProgress: RouteProgress) {
Expand Down Expand Up @@ -253,6 +262,51 @@ extension RouteMapViewController: NavigationMapViewDelegate {
let route = routeController.routeProgress.route
guard let coordinates = route.coordinates else { return nil }

if let userLocation = mapView.userLocation,
let style = mapView.style,
routeController.showCurrentWayNameLabel,
recenterButton.isHidden {

let streetsSources = style.sources.flatMap {
$0 as? MGLVectorSource
}.filter {
$0.isMapboxStreets
}
let streetsSourceIdentifiers = streetsSources.map {
$0.identifier
}
assert(!streetsSourceIdentifiers.isEmpty, "The option `showCurrentWayNameLabel` must contain the source `mapbox.mapbox-streets-v7`")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An option can’t contain a source. A style contains a source.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the style doesn’t contain Mapbox Streets source, we could add it.


if let mapboxSteetsSource = streetsSources.first, style.layer(withIdentifier: "roadLabelLayer") == nil{
let streetLabelLayer = MGLLineStyleLayer(identifier: RoadLabelLayerIdentifier, source: mapboxSteetsSource)
streetLabelLayer.sourceLayerIdentifier = "road_label"

// If the opacity is set to 0, the feature will be ignored in `mapView.visibleFeatures()`
streetLabelLayer.lineOpacity = MGLStyleValue(rawValue: 0.001)
streetLabelLayer.lineWidth = MGLStyleValue(rawValue: 10)
style.addLayer(streetLabelLayer)
}

if let userPuck = mapView.view(for: userLocation) {
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be great to adapt this UI to the user’s preferred language, but are we doing that in the turn banner and step table and on the map? I think it’d be more important to be consistent throughout the UI. We could use the runtime styling API to localize the map, with code very similar to this code. However, the Directions API doesn’t yet return localized way names, so the turn banner and step table are stuck in the local language.

streetsLanguages.contains(language) {
key += "_\(language)"
}

if let name = feature.attribute(forKey: key) as? String {
wayNameLabel.text = name
wayNameLabel.sizeToFit()
wayNameLabel.isHidden = false
}
}
}
}

var newCoordinate = location.coordinate
if routeController.snapsUserLocationAnnotationToRoute {
// Snap to route
Expand Down Expand Up @@ -338,6 +392,7 @@ extension RouteMapViewController: MGLMapViewDelegate {

if mode != .followWithCourse {
recenterButton.isHidden = false
wayNameLabel.isHidden = true
startResetTrackingModeTimer()
} else {
recenterButton.isHidden = true
Expand Down
37 changes: 37 additions & 0 deletions MapboxNavigation/StyleLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,41 @@ public class StyleLabel: UILabel {
textStyle = TextStyle(rawValue: inspectableTextStyle)!
}
}

@IBInspectable var topInset: CGFloat = 0.0
@IBInspectable var leftInset: CGFloat = 0.0
@IBInspectable var bottomInset: CGFloat = 0.0
@IBInspectable var rightInset: CGFloat = 0.0

var insets: UIEdgeInsets {
get {
return UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)
}
set {
topInset = newValue.top
leftInset = newValue.left
bottomInset = newValue.bottom
rightInset = newValue.right
}
}

override public func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}

override public func sizeThatFits(_ size: CGSize) -> CGSize {
var adjSize = super.sizeThatFits(size)
adjSize.width += leftInset + rightInset
adjSize.height += topInset + bottomInset

return adjSize
}

override public var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.width += leftInset + rightInset
contentSize.height += topInset + bottomInset

return contentSize
}
}