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

Overwrite rerouting logic #16

Merged
merged 4 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
- Removed setCamera() from updateCourseTracking()
- Added setCamera() to progressDidChange()
- Allow to customize rerouting logic by implementing RouteControllerDelegate#routeControllerGetDirections

## v2.0.0 (May 23, 2023)
- Upgrade minimum iOS version from 11.0 to 12.0.
Expand Down
39 changes: 22 additions & 17 deletions MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ extension RouteController: CLLocationManagerDelegate {
guard let strongSelf = self else {
return
}

// Update the rerouting state
strongSelf.isRerouting = false

if let error = error {
strongSelf.delegate?.routeController?(strongSelf, didFailToRerouteWith: error)
Expand Down Expand Up @@ -679,25 +682,27 @@ extension RouteController: CLLocationManagerDelegate {
}

func getDirections(from location: CLLocation, along progress: RouteProgress, completion: @escaping (_ mostSimilarRoute: Route?, _ routes: [Route]?, _ error: Error?)->Void) {
routeTask?.cancel()
let options = progress.reroutingOptions(with: location)

self.lastRerouteLocation = location

let complete = { [weak self] (mostSimilarRoute: Route?, routes: [Route]?, error: NSError?) in
self?.isRerouting = false
completion(mostSimilarRoute, routes, error)
}

routeTask = directions.calculate(options) {(waypoints, potentialRoutes, potentialError) in

guard let routes = potentialRoutes else {
return complete(nil, nil, potentialError)
if(delegate?.routeControllerGetDirections?(from: location, along: progress, completion: completion) != true){
// Run the default route calculation if the delegate method is not defined or does not return true
routeTask?.cancel()
let options = progress.reroutingOptions(with: location)

self.lastRerouteLocation = location

let complete = { [weak self] (mostSimilarRoute: Route?, routes: [Route]?, error: NSError?) in
completion(mostSimilarRoute, routes, error)
}

// Checks by comparing leg `name` properties and see if the edit distance is within threshold
let mostSimilar = routes.mostSimilar(to: progress.route)
return complete(mostSimilar ?? routes.first, routes, potentialError)
routeTask = directions.calculate(options) {(waypoints, potentialRoutes, potentialError) in

guard let routes = potentialRoutes else {
return complete(nil, nil, potentialError)
}

// Checks by comparing leg `name` properties and see if the edit distance is within threshold
let mostSimilar = routes.mostSimilar(to: progress.route)
return complete(mostSimilar ?? routes.first, routes, potentialError)
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions MapboxCoreNavigation/RouteControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ public protocol RouteControllerDelegate: AnyObject {
*/
@objc(routeControllerShouldDisableBatteryMonitoring:)
optional func routeControllerShouldDisableBatteryMonitoring(_ routeController: RouteController) -> Bool

/**
Allows to customise the calculation of a route.

If you want to overwrite the defult rerouting loigc, return true.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
If you want to overwrite the defult rerouting loigc, return true.
If you want to overwrite the default rerouting logic, return true.


- parameter from: The current olocation of the user
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- parameter from: The current olocation of the user
- parameter from: The current location of the user

- parameter along: The route progress
- parameter completion: Callbackc function when either the route was sucessfully caluclated or if there was an error
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- parameter completion: Callbackc function when either the route was sucessfully caluclated or if there was an error
- parameter completion: Callback function when either the route was successfully calculated or if there was an error

- return: True to preferent the route controller from running its own rerouting logic
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- return: True to preferent the route controller from running its own rerouting logic
- return: True to prevent the route controller from running its own rerouting logic

*/
@objc(routeControllerGetDirections:along:completion:)
optional func routeControllerGetDirections(from location: CLLocation, along progress: RouteProgress, completion: @escaping (_ mostSimilarRoute: Route?, _ routes: [Route]?, _ error: Error?)->Void) -> Bool
}
Loading