Skip to content

Commit

Permalink
remove all data after cancel navigation, improve documentation and co…
Browse files Browse the repository at this point in the history
…mment
  • Loading branch information
ShanMa1991 committed Jun 21, 2021
1 parent b229b82 commit 130e130
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions Navigation-Examples/Examples/Custom-User-Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
var routes: [Route]? {
didSet {
guard let routes = routes, let currentRoute = routes.first else {
navigationMapView.removeRoutes()
navigationMapView.removeWaypoints()
navigationMapView?.removeRoutes()
navigationMapView?.removeRouteDurations()
navigationMapView?.removeWaypoints()
waypoints.removeAll()
return
}
Expand All @@ -53,6 +54,12 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
}
}

var startButtonHighlighted:Bool = false {
didSet {
startButton.backgroundColor = startButtonHighlighted ? .clear : .blue
}
}

private let startButton = UIButton()
private let clearButton = UIButton()

Expand Down Expand Up @@ -97,7 +104,8 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg

func setupStartButton() {
startButton.setTitle("Start", for: .normal)
startButton.backgroundColor = .blue
startButton.addTarget(self, action: #selector(startButtonChangeColor), for: .touchUpInside)
startButtonHighlighted = false
startButton.layer.cornerRadius = 5
startButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

Expand All @@ -115,6 +123,11 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
navigationMapView.addGestureRecognizer(longPressGestureRecognizer)
}


@objc private func startButtonChangeColor() {
startButtonHighlighted = !startButtonHighlighted
}

@objc func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
guard gesture.state == .began else { return }
let gestureLocation = gesture.location(in: navigationMapView)
Expand Down Expand Up @@ -160,10 +173,7 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
}

@objc func clearMap(_ sender: Any) {
navigationMapView?.removeRoutes()
navigationMapView?.removeRouteDurations()
navigationMapView?.removeWaypoints()
waypoints.removeAll()
routes = nil
}

@objc func performAction(_ sender: Any) {
Expand All @@ -177,12 +187,13 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
let courseView: ActionHandler = { _ in self.setupCourseView() }
let puck2D: ActionHandler = { _ in self.setupPuck2D() }
let puck3D: ActionHandler = { _ in self.setupPuck3D() }
let cancel: ActionHandler = { _ in self.startButtonHighlighted = false }

let actionPayloads: [(String, UIAlertAction.Style, ActionHandler?)] = [
("Default Course View", .default, courseView),
("2D Arrow Puck", .default, puck2D),
("3D Car Puck", .default, puck3D),
("Cancel", .cancel, nil)
("Cancel", .cancel, cancel)
]

actionPayloads
Expand All @@ -199,8 +210,8 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg

func setupCourseView() {
// Given configuration to the `UserLocationStyle.courseView` through the customizing of `UserPuckCourseView`. Both `UserPuckCourseView` and `UserHaloView` are subclassable.
// By defualt `navigationMapView.UserLocationStyle.`courseView` will be `navigationMapView.userCourseView`.
presentNavigationViewconstroller()
// By default `NavigationMapView.userLocationStyle` property is set to `UserLocationStyle.courseView(_:)`.
presentNavigationViewController()
}

func setupPuck2D() {
Expand All @@ -212,18 +223,18 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
}

let userLocationStyle = UserLocationStyle.puck2D(configuration: puck2DConfiguration)
presentNavigationViewconstroller(userLocationStyle)
presentNavigationViewController(userLocationStyle)
}

func setupPuck3D() {
// It's required to provide a `Puck3DConfiguration` to the `UserLocationStyle.puck3D`.
let uri = Bundle.main.url(forResource: "race_car_model",
withExtension: "gltf")
// Instantiating the model. The position is the coordinates of the model in `[longitude, latitude]`format.
// Instantiating the model. The position is the coordinates of the model in `[longitude, latitude]` format.
let myModel = Model(uri: uri,
position: [-122.396152, 37.79129],
orientation: [0, 0, 90])
// Setting an expression to scale the model based on camera zoom
// Setting an expression to scale the model based on camera zoom
let scalingExpression = Exp(.interpolate) {
Exp(.linear)
Exp(.zoom)
Expand Down Expand Up @@ -255,10 +266,10 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg

let puck3DConfiguration = Puck3DConfiguration(model: myModel, modelScale: .expression(scalingExpression))
let userLocationStyle = UserLocationStyle.puck3D(configuration: puck3DConfiguration)
presentNavigationViewconstroller(userLocationStyle)
presentNavigationViewController(userLocationStyle)
}

func presentNavigationViewconstroller(_ userLocationStyle: UserLocationStyle? = nil ) {
func presentNavigationViewController(_ userLocationStyle: UserLocationStyle? = nil) {
guard let route = currentRoute, let navigationRouteOptions = navigationRouteOptions else { return }

let navigationService = MapboxNavigationService(route: route,
Expand All @@ -274,7 +285,7 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
navigationViewController.delegate = self
navigationViewController.modalPresentationStyle = .fullScreen

// If not customizing the `userLocationStyle`, it defaults as the `navigationMapView.userCourseView`.
// If not customizing the `NavigationMapView.userLocationStyle`, it defaults as the `UserLocationStyle.courseView(_:)`.
if let userLocationStyle = userLocationStyle {
navigationViewController.navigationMapView?.userLocationStyle = userLocationStyle
}
Expand All @@ -288,6 +299,7 @@ class CustomUserLocationViewController: UIViewController, NavigationMapViewDeleg
}

func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController, byCanceling canceled: Bool) {
routes = nil
dismiss(animated: true, completion: nil)
if navigationMapView == nil {
navigationMapView = NavigationMapView(frame: view.bounds)
Expand Down

0 comments on commit 130e130

Please sign in to comment.