Skip to content

Commit 24d6403

Browse files
authored
Add delegate methods for tapping route duration. (#4133)
1 parent b490cbb commit 24d6403

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Map
1717

1818
* `NavigationMapView.removeAlternativeRoutes()` and `NavigationMapView.removeContinuousAlternativeRoutesDurations()` were made public to provide a way to remove previously shown alternative routes and alternative routes duration annotations, respectively. ([#4134](https://github.com/mapbox/mapbox-navigation-ios/pull/4134))
19+
* Fixed an issue where tapping on a route duration annotation that overlaps a different route would cause the wrong route to be passed into `NavigationMapViewDelegate.navigationMapView(_:didSelect:)` or `NavigationMapViewDelegate.navigationMapView(_:didSelect:)`. ([#4133](https://github.com/mapbox/mapbox-navigation-ios/pull/4133))
1920

2021
### Banners and guidance instructions
2122

Example/ViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,8 @@ extension ViewController: NavigationMapViewDelegate {
744744
}
745745

746746
func navigationMapView(_ mapView: NavigationMapView, didSelect route: Route) {
747-
guard let index = routes?.firstIndex(where: { $0 === route }) else { return }
747+
guard let index = routes?.firstIndex(where: { $0 === route }),
748+
index != indexedRouteResponse?.routeIndex else { return }
748749
indexedRouteResponse?.routeIndex = index
749750
}
750751

Sources/MapboxNavigation/NavigationMapView.swift

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ open class NavigationMapView: UIView {
13231323
"text": .string(labelText),
13241324
"imageName": .string(imageName),
13251325
"sortOrder": .number(Double(isSelected ? index : -index)),
1326+
"routeIndex": .number(Double(index))
13261327
]
13271328

13281329
return feature
@@ -2120,12 +2121,59 @@ open class NavigationMapView: UIView {
21202121
if let selected = waypointTest?.first {
21212122
delegate?.navigationMapView(self, didSelect: selected)
21222123
return
2123-
} else if let routes = self.routes(closeTo: tapPoint),
2124-
let selectedRoute = routes.first {
2125-
delegate?.navigationMapView(self, didSelect: selectedRoute)
2126-
} else if let alternativeRoutes = self.continuousAlternativeRoutes(closeTo: tapPoint),
2127-
let selectedRoute = alternativeRoutes.first {
2128-
delegate?.navigationMapView(self, didSelect: selectedRoute)
2124+
}
2125+
2126+
route(at: tapPoint) { [weak self] (routeFoundByPoint) in
2127+
guard !routeFoundByPoint, let self = self else { return }
2128+
if let routes = self.routes(closeTo: tapPoint),
2129+
let selectedRoute = routes.first {
2130+
self.delegate?.navigationMapView(self, didSelect: selectedRoute)
2131+
} else if let alternativeRoutes = self.continuousAlternativeRoutes(closeTo: tapPoint),
2132+
let selectedRoute = alternativeRoutes.first {
2133+
self.delegate?.navigationMapView(self, didSelect: selectedRoute)
2134+
}
2135+
}
2136+
}
2137+
2138+
func route(at point: CGPoint, completion: @escaping (Bool) -> Void) {
2139+
let group = DispatchGroup()
2140+
var routeFoundByPoint: Bool = false
2141+
let layerIds: [String] = [
2142+
LayerIdentifier.routeDurationAnnotationsLayer,
2143+
LayerIdentifier.continuousAlternativeRoutesDurationAnnotationsLayer]
2144+
2145+
for layerId in layerIds {
2146+
group.enter()
2147+
let options = RenderedQueryOptions(layerIds: [layerId], filter: nil)
2148+
routeIndexFromMapQuery(with: options, at: point) { [weak self] (routeIndex) in
2149+
defer { group.leave() }
2150+
guard let self = self, let routeIndex = routeIndex else { return }
2151+
if layerId == layerIds.first {
2152+
if let route = self.routes?[safe: routeIndex] {
2153+
routeFoundByPoint = true
2154+
self.delegate?.navigationMapView(self, didSelect: route)
2155+
}
2156+
} else if let alternativeRoute = self.continuousAlternatives?[safe: routeIndex] {
2157+
routeFoundByPoint = true
2158+
self.delegate?.navigationMapView(self, didSelect: alternativeRoute)
2159+
}
2160+
}
2161+
}
2162+
2163+
group.notify(queue: DispatchQueue.main) {
2164+
completion(routeFoundByPoint)
2165+
}
2166+
}
2167+
2168+
func routeIndexFromMapQuery(with options: RenderedQueryOptions, at point: CGPoint, completion: @escaping (Int?) -> Void) {
2169+
mapView.mapboxMap.queryRenderedFeatures(with: [point], options: options) { result in
2170+
if case .success(let queriedFeatures) = result,
2171+
let indexValue = queriedFeatures.first?.feature.properties?["routeIndex"] as? JSONValue,
2172+
case .number(let routeIndex) = indexValue {
2173+
completion(Int(routeIndex))
2174+
} else {
2175+
completion(nil)
2176+
}
21292177
}
21302178
}
21312179

0 commit comments

Comments
 (0)