Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ binary "https://www.mapbox.com/ios-sdk/MapboxAccounts.json" ~> 2.3.0
binary "https://api.mapbox.com/downloads/v2/carthage/mobile-navigation-native/MapboxNavigationNative.json" ~> 18.0.2
binary "https://api.mapbox.com/downloads/v2/carthage/mobile-maps/mapbox-ios-sdk-dynamic.json" ~> 6.0
binary "https://api.mapbox.com/downloads/v2/carthage/mapbox-common/MapboxCommon-ios.json" == 4.0.0
github "mapbox/mapbox-directions-swift" == 1.0.0-rc.2
github "mapbox/mapbox-directions-swift" "maxim/459-expose-duration-typical"
github "mapbox/turf-swift" ~> 0.5
github "mapbox/mapbox-events-ios" ~> 0.10
github "ceeK/Solar" ~> 2.1.0
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ github "Udumft/SnappyShrimp" "66f3e3ba70370ad5e889ac8ed72a8730ca79958e"
github "ceeK/Solar" "2.1.0"
github "linksmt/OHHTTPStubs" "563f48d3fab84ef04639649c770b00f4fa502cca"
github "mapbox/MapboxGeocoder.swift" "v0.10.2"
github "mapbox/mapbox-directions-swift" "v1.0.0-rc.2"
github "mapbox/mapbox-directions-swift" "0251abc736cc08d7fd1071977745ce640e359758"
github "mapbox/mapbox-events-ios" "v0.10.3"
github "mapbox/mapbox-speech-swift" "v0.3.1"
github "mapbox/turf-swift" "v0.5.0"
Expand Down
1 change: 1 addition & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ class ViewController: UIViewController {
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
mapView.showsUserHeadingIndicator = true
mapView.showsDelay = true
}

func uninstall(_ mapView: NavigationMapView) {
Expand Down
4 changes: 4 additions & 0 deletions MapboxNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
6441B16A1EFC64E50076499F /* WaypointConfirmationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6441B1691EFC64E50076499F /* WaypointConfirmationViewController.swift */; };
64847A041F04629D003F3A69 /* Feedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64847A031F04629D003F3A69 /* Feedback.swift */; };
7C12F2D8225B7C320010A931 /* DCA-Arboretum-dummy-faster-route.json in Resources */ = {isa = PBXBuildFile; fileRef = 7C12F2D7225B7C310010A931 /* DCA-Arboretum-dummy-faster-route.json */; };
8A6C963D251140AE00B9B587 /* DelayInformationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A6C963C251140AE00B9B587 /* DelayInformationView.swift */; };
8AA849E924E722410008EE59 /* WaypointStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA849E824E722410008EE59 /* WaypointStyle.swift */; };
8D07C5A820B612310093D779 /* EmptyStyle.json in Resources */ = {isa = PBXBuildFile; fileRef = 8D07C5A720B612310093D779 /* EmptyStyle.json */; };
8D1A5CD2212DDFCD0059BA4A /* DispatchTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D1A5CD1212DDFCD0059BA4A /* DispatchTimer.swift */; };
Expand Down Expand Up @@ -864,6 +865,7 @@
6441B1691EFC64E50076499F /* WaypointConfirmationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WaypointConfirmationViewController.swift; path = Example/WaypointConfirmationViewController.swift; sourceTree = "<group>"; };
64847A031F04629D003F3A69 /* Feedback.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Feedback.swift; sourceTree = "<group>"; };
7C12F2D7225B7C310010A931 /* DCA-Arboretum-dummy-faster-route.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "DCA-Arboretum-dummy-faster-route.json"; sourceTree = "<group>"; };
8A6C963C251140AE00B9B587 /* DelayInformationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DelayInformationView.swift; sourceTree = "<group>"; };
8AA849E824E722410008EE59 /* WaypointStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaypointStyle.swift; sourceTree = "<group>"; };
8B808F852487CFEC00EEE453 /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/Main.strings; sourceTree = "<group>"; };
8B808F862487CFEC00EEE453 /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/Navigation.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1357,6 +1359,7 @@
F4BF512D24EAD7A50066A49B /* FeedbackSubtypeCollectionViewCell.swift */,
5A1C075724BDEB44000A6330 /* PassiveLocationManager.swift */,
F4C5A26E24EF1D16004ED0DD /* FeedbackSubtypeViewController.swift */,
8A6C963C251140AE00B9B587 /* DelayInformationView.swift */,
);
path = MapboxNavigation;
sourceTree = "<group>";
Expand Down Expand Up @@ -2476,6 +2479,7 @@
35B1E2951F1FF8EC00A13D32 /* UserCourseView.swift in Sources */,
35B5A47E1FFFDCE5000A3C8D /* NavigationViewLayout.swift in Sources */,
8DDBFCA32205016E0064DEBB /* NavigationCustomizable.swift in Sources */,
8A6C963D251140AE00B9B587 /* DelayInformationView.swift in Sources */,
3531C2701F9E095400D92F9A /* InstructionsBannerView.swift in Sources */,
2B8098432411447E00FED452 /* MultiplexedSpeechSynthesizer.swift in Sources */,
35DA85791FC45787004092EC /* StatusView.swift in Sources */,
Expand Down
34 changes: 34 additions & 0 deletions MapboxNavigation/DelayInformationView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import UIKit

class DelayInformationView: UIView {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't really like creating DelayInformationView which is then later converted to UIImage so that it's possible to use it in MGLSymbolStyleLayer (there are some additional issues like text overlapping etc).

On the other hand seems that MGLPointAnnotation will not be performant enough when using large amount of delay annotations. It'll also require implementation of MGMapViewDelegate methods inside NavigationMapView.

@1ec5 if you have any better ideas please let me know.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

/cc @langsmith ^^^ in case we've not considered this for the Android impl.


override func draw(_ rect: CGRect) {
let lineWidth: CGFloat = 2
let cornerRadius: CGFloat = 3
let tipLength: CGFloat = 6
let fillColor: UIColor = .red
let strokeColor: UIColor = .black

let rect = bounds.insetBy(dx: lineWidth / 2, dy: lineWidth / 2)
let path = UIBezierPath()
path.move(to: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY - tipLength))
path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY - tipLength - cornerRadius), controlPoint: CGPoint(x: rect.minX, y: rect.maxY - tipLength))
path.addLine(to: CGPoint(x: rect.minX, y: rect.minY + cornerRadius))
path.addQuadCurve(to: CGPoint(x: rect.minX + cornerRadius, y: rect.minY), controlPoint: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY))
path.addQuadCurve(to: CGPoint(x: rect.maxX, y: rect.minY + cornerRadius), controlPoint: CGPoint(x: rect.maxX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - tipLength - cornerRadius))
path.addQuadCurve(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.maxY - tipLength), controlPoint: CGPoint(x: rect.maxX, y: rect.maxY - tipLength))
path.addLine(to: CGPoint(x: rect.midX + tipLength, y: rect.maxY - tipLength))
path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.midX - tipLength, y: rect.maxY - tipLength))
path.close()

fillColor.setFill()
path.fill()

strokeColor.setStroke()
path.lineWidth = lineWidth
path.stroke()
}
}
134 changes: 132 additions & 2 deletions MapboxNavigation/NavigationMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {
*/
public weak var courseTrackingDelegate: NavigationMapViewCourseTrackingDelegate?

/**
Controls whether delays are shown along the route line.
*/
public var showsDelay: Bool = false {
Copy link
Copy Markdown
Contributor Author

@MaximAlien MaximAlien Sep 16, 2020

Choose a reason for hiding this comment

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

For now boolean flag is used to show/hide delay information views. Since delay information is provided for Route, Leg and Step we might want to provide API which allows to show delay for each of those. As per comment here default minimum delay threshold value should be set to 4 minutes.

didSet {
guard let style = style else { return }

if showsDelay {
guard let routes = routes else { return }
let delayInformationSource = addDelayInformationSource(style, routes: routes)
addDelayLayer(style, source: delayInformationSource)
} else {
if let delaySource = style.source(withIdentifier: SourceIdentifier.delay) {
style.removeSource(delaySource)
}

if let delayLayer = style.layer(withIdentifier: StyleLayerIdentifier.delay) {
style.removeLayer(delayLayer)
}
}
}
}

let sourceOptions: [MGLShapeSourceOption: Any] = [.maximumZoomLevel: 16]

struct SourceIdentifier {
Expand All @@ -78,6 +101,8 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {
static let mainRouteCasing = "\(identifierNamespace).mainRouteCasing"

static let buildingExtrusion = "\(identifierNamespace).buildingExtrusion"

static let delay = "\(identifierNamespace).delay"
}

struct StyleLayerIdentifier {
Expand All @@ -103,6 +128,8 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {
static let instructionCircle = "\(identifierNamespace).instructionCircle"

static let buildingExtrusion = "\(identifierNamespace).buildingExtrusion"

static let delay = "\(identifierNamespace).delay"
}

// MARK: - Instance Properties
Expand Down Expand Up @@ -480,6 +507,11 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {
let mainRouteCasingLayer = addMainRouteCasingLayer(style, source: mainRouteCasingSource, lineGradient: routeCasingGradient(0.0), below: mainRouteLayer)
let alternativeRoutesLayer = addAlternativeRoutesLayer(style, source: allRoutesSource, below: mainRouteCasingLayer)
addAlternativeRoutesCasingLayer(style, source: allRoutesSource, below: alternativeRoutesLayer)

if showsDelay {
let delayInformationSource = addDelayInformationSource(style, routes: routes)
addDelayLayer(style, source: delayInformationSource)
}
}

// MARK: - Route line insertion methods
Expand Down Expand Up @@ -509,6 +541,24 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {

return mainRouteCasingSource
}

func addDelayInformationSource(_ style: MGLStyle, routes: [Route]) -> MGLSource {
if let delayInformationSource = style.source(withIdentifier: SourceIdentifier.delay) as? MGLShapeSource {
return delayInformationSource
}

let delayInformationSource = MGLShapeSource(identifier: SourceIdentifier.delay, features: delayFeatures(routes), options: nil)
style.addSource(delayInformationSource)

// TODO: Improve delay view size calculation for various delay values.
let delayInformationView = DelayInformationView(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
delayInformationView.backgroundColor = .clear
if let imageRepresentation = delayInformationView.imageRepresentation {
style.setImage(imageRepresentation, forName: "delay_information")
}

return delayInformationSource
}

@discardableResult func addMainRouteLayer(_ style: MGLStyle, source: MGLSource, lineGradient: NSExpression?) -> MGLStyleLayer {
let customMainRouteLayer = navigationMapViewDelegate?.navigationMapView(self,
Expand Down Expand Up @@ -655,6 +705,36 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {

return alternativeRoutesCasingLayer
}

@discardableResult func addDelayLayer(_ style: MGLStyle, source: MGLSource) -> MGLStyleLayer {
if let delayLayer = style.layer(withIdentifier: StyleLayerIdentifier.delay) {
return delayLayer
}

let opacityStops = [
11: 0.0,
13: 0.7,
15: 1
]

let delayLayer = MGLSymbolStyleLayer(identifier: StyleLayerIdentifier.delay, source: source)
delayLayer.iconImageName = NSExpression(forConstantValue: "delay_information")
delayLayer.iconScale = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [11: 0.05, 13: 0.5, 15: 1])
delayLayer.iconOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", opacityStops)
delayLayer.iconAnchor = NSExpression(forConstantValue: NSValue(mglIconAnchor: .bottom))
delayLayer.iconHaloColor = NSExpression(forConstantValue: UIColor.black)
delayLayer.text = NSExpression(forKeyPath: "delay")
delayLayer.textColor = NSExpression(forConstantValue: UIColor.white)
delayLayer.textFontSize = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [11: 0.05, 13: 7, 15: 15])
delayLayer.textOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", opacityStops)
delayLayer.maximumTextWidth = NSExpression(forConstantValue: 80.0)
delayLayer.textAnchor = NSExpression(forConstantValue: NSValue(mglTextAnchor: .bottom))
delayLayer.textOffset = NSExpression(forConstantValue: CGVector(dx: 0, dy: -1))

style.addLayer(delayLayer)

return delayLayer
}

// MARK: - Vanishing route line methods

Expand Down Expand Up @@ -823,11 +903,13 @@ open class NavigationMapView: MGLMapView, UIGestureRecognizerDelegate {
StyleLayerIdentifier.mainRoute,
StyleLayerIdentifier.mainRouteCasing,
StyleLayerIdentifier.alternativeRoutes,
StyleLayerIdentifier.alternativeRoutesCasing
StyleLayerIdentifier.alternativeRoutesCasing,
StyleLayerIdentifier.delay
].compactMap { style.layer(withIdentifier: $0) })
style.remove(Set([
SourceIdentifier.allRoutes,
SourceIdentifier.mainRouteCasing
SourceIdentifier.mainRouteCasing,
SourceIdentifier.delay
].compactMap { style.source(withIdentifier: $0) }))

routes = nil
Expand Down Expand Up @@ -1525,3 +1607,51 @@ extension NavigationMapView {
highlightedBuildingsLayer.fillExtrusionOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", opacityStops)
}
}

// MARK: - Delay Information

extension NavigationMapView {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since NavigationMapView now contains several features we can expose them as separate extension files (e.g. for route lines, building highlighting, vanishing route line, delay information).


func delay(_ expectedTravelTime: TimeInterval, typicalTravelTime: TimeInterval?) -> String {
var delay = ""
// TODO: Handle case when typicalTravelTime is larger than expectedTravelTime.
// TODO: Improve size calculation for various delays.
if let typicalTravelTime = typicalTravelTime, expectedTravelTime >= typicalTravelTime {
let timeDifference = Int(expectedTravelTime - typicalTravelTime)
let (hours, minutes) = (timeDifference / 3600, (timeDifference % 3600) / 60)

if hours > 0 {
delay += "+\(hours) hr "
}

if minutes > 0 {
if delay.isEmpty { delay += "+" }
delay += "\(minutes) min"
}
}

return delay
}

func delayFeatures(_ routes: [Route]) -> [MGLPointFeature] {
var features = [MGLPointFeature]()

routes.forEach {
$0.legs.forEach {
$0.steps.forEach {
let totalDelay = delay($0.expectedTravelTime, typicalTravelTime: $0.typicalTravelTime)
if !totalDelay.isEmpty {
let pointFeature = MGLPointFeature()
pointFeature.coordinate = $0.maneuverLocation
pointFeature.attributes = [
"delay": totalDelay
]
features.append(pointFeature)
}
}
}
}

return features
}
}