From 2d8256e2da4dd7d5bb4c609bcf87f58b7b0356e3 Mon Sep 17 00:00:00 2001 From: Karol Sotomski Date: Wed, 17 Jul 2024 10:20:12 +0200 Subject: [PATCH 1/3] feat: Open RouteController to custom location snapping --- MapboxCoreNavigation/RouteController.swift | 8 +++++++- MapboxCoreNavigation/RouteControllerDelegate.swift | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/MapboxCoreNavigation/RouteController.swift b/MapboxCoreNavigation/RouteController.swift index f1c37e1a..fae71ce8 100644 --- a/MapboxCoreNavigation/RouteController.swift +++ b/MapboxCoreNavigation/RouteController.swift @@ -230,7 +230,13 @@ open class RouteController: NSObject, Router { - important: If the rawLocation is outside of the route snapping tolerances, this value is nil. */ var snappedLocation: CLLocation? { - self.rawLocation?.snapped(to: self.routeProgress.currentLegProgress) + let snappedDefault = self.rawLocation?.snapped(to: self.routeProgress.currentLegProgress) + guard let raw = self.rawLocation else { return snappedDefault } + + let snappedCustom = self.delegate?.routeControllerSnap?( + rawLocation: raw, + snappedByDefaultTo: snappedDefault) + return snappedCustom ?? snappedDefault } var heading: CLHeading? diff --git a/MapboxCoreNavigation/RouteControllerDelegate.swift b/MapboxCoreNavigation/RouteControllerDelegate.swift index 6f0685f9..594e7923 100644 --- a/MapboxCoreNavigation/RouteControllerDelegate.swift +++ b/MapboxCoreNavigation/RouteControllerDelegate.swift @@ -125,4 +125,13 @@ public protocol RouteControllerDelegate: AnyObject { */ @objc(routeControllerGetDirections:along:completion:) optional func routeControllerGetDirections(from location: CLLocation, along progress: RouteProgress, completion: @escaping (_ mostSimilarRoute: Route?, _ routes: [Route]?, _ error: Error?) -> Void) -> Bool + + /** + Allows to customize the snapping of raw and pre-snapped location. If no implementation is provided, the default snapping will be used. + + - parameter rawLocation: The raw location from the controller + - returns: The snapped location or nil if snapping were impossible. + */ + @objc(routeControllerSnapLocation:snappedByDefaultTo:) + optional func routeControllerSnap(rawLocation: CLLocation, snappedByDefaultTo snappedLocation: CLLocation?) -> CLLocation? } From 851903b9e4d429f0d75694864129de516ac15a08 Mon Sep 17 00:00:00 2001 From: Karol Sotomski Date: Thu, 18 Jul 2024 15:50:57 +0200 Subject: [PATCH 2/3] feat: Pass only raw location to the delegate --- MapboxCoreNavigation/RouteController.swift | 11 +++++------ MapboxCoreNavigation/RouteControllerDelegate.swift | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/MapboxCoreNavigation/RouteController.swift b/MapboxCoreNavigation/RouteController.swift index fae71ce8..92c3b332 100644 --- a/MapboxCoreNavigation/RouteController.swift +++ b/MapboxCoreNavigation/RouteController.swift @@ -230,13 +230,12 @@ open class RouteController: NSObject, Router { - important: If the rawLocation is outside of the route snapping tolerances, this value is nil. */ var snappedLocation: CLLocation? { - let snappedDefault = self.rawLocation?.snapped(to: self.routeProgress.currentLegProgress) - guard let raw = self.rawLocation else { return snappedDefault } + guard let raw = self.rawLocation else { + return nil + } - let snappedCustom = self.delegate?.routeControllerSnap?( - rawLocation: raw, - snappedByDefaultTo: snappedDefault) - return snappedCustom ?? snappedDefault + let customSnap = self.delegate?.routeControllerSnap?(rawLocation: raw) + return customSnap ?? raw.snapped(to: self.routeProgress.currentLegProgress) } var heading: CLHeading? diff --git a/MapboxCoreNavigation/RouteControllerDelegate.swift b/MapboxCoreNavigation/RouteControllerDelegate.swift index 594e7923..024bb379 100644 --- a/MapboxCoreNavigation/RouteControllerDelegate.swift +++ b/MapboxCoreNavigation/RouteControllerDelegate.swift @@ -132,6 +132,6 @@ public protocol RouteControllerDelegate: AnyObject { - parameter rawLocation: The raw location from the controller - returns: The snapped location or nil if snapping were impossible. */ - @objc(routeControllerSnapLocation:snappedByDefaultTo:) - optional func routeControllerSnap(rawLocation: CLLocation, snappedByDefaultTo snappedLocation: CLLocation?) -> CLLocation? + @objc(routeControllerSnapLocation:) + optional func routeControllerSnap(rawLocation: CLLocation) -> CLLocation? } From 867d93949ca55519bd3fbf846e6cb25766d4d12f Mon Sep 17 00:00:00 2001 From: Karol Sotomski Date: Thu, 18 Jul 2024 17:27:50 +0200 Subject: [PATCH 3/3] chore: Modify the change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de7aca27..30772394 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ self.startNavigation(with: route, animated: false) #endif ``` +* Custom location snapping in the `RouteController` via the delegate ## 3.0.0 (Jun 15, 2024) * The `speak` method in `RouteVoiceController` can be used without a given `RouteProgress` or the `RouteProgress` can explicitly ignored so that it will not be added to the voice instruction.