Skip to content

Commit

Permalink
[ios] Update custom waypoints example (#7688)
Browse files Browse the repository at this point in the history
  • Loading branch information
volkdmitri authored Dec 23, 2024
1 parent 9c5e26d commit 2639d99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "intermediate_waypoint.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
49 changes: 43 additions & 6 deletions Examples/AdditionalExamples/Examples/Custom-Waypoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import MapboxNavigationUIKit
import UIKit

final class CustomWaypointsViewController: UIViewController {
fileprivate static let intermediateWaypointImageId = "intermediateWaypointImageId"

private let mapboxNavigationProvider = MapboxNavigationProvider(
coreConfig: .init(
locationSource: simulationIsEnabled ? .simulation(
Expand Down Expand Up @@ -55,6 +57,8 @@ final class CustomWaypointsViewController: UIViewController {
}

private var startButton: UIButton!
private var mapStyleLoadedCancellable: Cancelable?
private var mapStyleLoadedNavigationVCCancellable: Cancelable?

// MARK: - UIViewController lifecycle methods

Expand All @@ -72,6 +76,10 @@ final class CustomWaypointsViewController: UIViewController {
)
navigationMapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
navigationMapView.delegate = self
mapStyleLoadedCancellable = navigationMapView.mapView.mapboxMap.onStyleLoaded
.observe { [weak navigationMapView] _ in
navigationMapView?.addIntermediateWaypointImage()
}
view.addSubview(navigationMapView)

startButton = UIButton()
Expand Down Expand Up @@ -120,6 +128,10 @@ final class CustomWaypointsViewController: UIViewController {
)
navigationViewController.delegate = self
navigationViewController.modalPresentationStyle = .fullScreen
mapStyleLoadedNavigationVCCancellable = navigationViewController.navigationMapView?.mapView.mapboxMap
.onStyleLoaded.observe { [weak navigationViewController] _ in
navigationViewController?.navigationMapView?.addIntermediateWaypointImage()
}

startButton.isHidden = true
present(navigationViewController, animated: true)
Expand Down Expand Up @@ -176,34 +188,58 @@ final class CustomWaypointsViewController: UIViewController {
}
})
symbolLayer.textSize = .constant(.init(10))
symbolLayer.textOpacity = .expression(Exp(.switchCase) {
let opacity = Exp(.switchCase) {
Exp(.any) {
Exp(.get) {
"waypointCompleted"
}
}
0.5
1
})
}
symbolLayer.textOpacity = .expression(opacity)
symbolLayer.textHaloWidth = .constant(.init(0.25))
symbolLayer.textHaloColor = .constant(.init(UIColor.black))
symbolLayer.iconImage = .expression(Exp(.get) { "waypointIconImage" })
symbolLayer.iconAnchor = .constant(.bottom)
symbolLayer.iconOffset = .constant([0, 15])
symbolLayer.iconOpacity = .expression(opacity)
return symbolLayer
}

private func customWaypointShape(shapeFor waypoints: [Waypoint], legIndex: Int) -> FeatureCollection {
var features = [Turf.Feature]()
for (waypointIndex, waypoint) in waypoints.enumerated() {
var feature = Feature(geometry: .point(Point(waypoint.coordinate)))
feature.properties = [
"waypointCompleted": .boolean(waypointIndex < legIndex),
"name": .number(Double(waypointIndex + 1)),
]
var properties: [String: JSONValue] = [:]
properties["name"] = .number(Double(waypointIndex + 1))
properties["waypointCompleted"] = .boolean(waypointIndex <= legIndex)
properties["waypointIconImage"] = waypointIndex > 0 && waypointIndex < waypoints.count - 1
? .string(Self.intermediateWaypointImageId)
: nil
feature.properties = properties

features.append(feature)
}
return FeatureCollection(features: features)
}
}

extension NavigationMapView {
fileprivate func addIntermediateWaypointImage() {
guard !mapView.mapboxMap.imageExists(withId: CustomWaypointsViewController.intermediateWaypointImageId) else {
return
}

try! mapView.mapboxMap.addImage(
UIImage(named: "intermediate_waypoint")!,
id: CustomWaypointsViewController.intermediateWaypointImageId,
stretchX: [],
stretchY: []
)
}
}

// MARK: Delegate methods

extension CustomWaypointsViewController: NavigationMapViewDelegate {
Expand Down Expand Up @@ -261,6 +297,7 @@ extension CustomWaypointsViewController: NavigationViewControllerDelegate {
_ navigationViewController: NavigationViewController,
byCanceling canceled: Bool
) {
mapStyleLoadedNavigationVCCancellable?.cancel()
dismiss(animated: true, completion: nil)
}
}

0 comments on commit 2639d99

Please sign in to comment.