Compare commits

...

2 commits

Author SHA1 Message Date
Eugene Burmakin
0de2cb26b8 Add fallbacks when retrieving full route features to handle cases where source data access methods vary. 2026-01-07 00:18:26 +01:00
Eugene Burmakin
4874245139 Fix route highlighting 2026-01-06 23:30:18 +01:00

View file

@ -135,20 +135,24 @@ export class EventHandlers {
* Handle route hover * Handle route hover
*/ */
handleRouteHover(e) { handleRouteHover(e) {
const feature = e.features[0] const clickedFeature = e.features[0]
if (!feature) return if (!clickedFeature) return
const routesLayer = this.controller.layerManager.getLayer('routes') const routesLayer = this.controller.layerManager.getLayer('routes')
if (!routesLayer) return if (!routesLayer) return
// Get the full feature from source (not the clipped tile version)
// Fallback to clipped feature if full feature not found
const fullFeature = this._getFullRouteFeature(clickedFeature.properties) || clickedFeature
// If a route is selected and we're hovering over a different route, show both // If a route is selected and we're hovering over a different route, show both
if (this.selectedRouteFeature) { if (this.selectedRouteFeature) {
// Check if we're hovering over the same route that's selected // Check if we're hovering over the same route that's selected
const isSameRoute = this._areFeaturesSame(this.selectedRouteFeature, feature) const isSameRoute = this._areFeaturesSame(this.selectedRouteFeature, fullFeature)
if (!isSameRoute) { if (!isSameRoute) {
// Show both selected and hovered routes // Show both selected and hovered routes
const features = [this.selectedRouteFeature, feature] const features = [this.selectedRouteFeature, fullFeature]
routesLayer.setHoverRoute({ routesLayer.setHoverRoute({
type: 'FeatureCollection', type: 'FeatureCollection',
features: features features: features
@ -158,9 +162,9 @@ export class EventHandlers {
} }
} else { } else {
// No selection, just show hovered route // No selection, just show hovered route
routesLayer.setHoverRoute(feature) routesLayer.setHoverRoute(fullFeature)
// Create markers for hovered route // Create markers for hovered route
this._createRouteMarkers(feature) this._createRouteMarkers(fullFeature)
} }
} }
@ -183,6 +187,47 @@ export class EventHandlers {
} }
} }
/**
* Get full route feature from source data (not clipped tile version)
* MapLibre returns clipped geometries from queryRenderedFeatures()
* We need the full geometry from the source for proper highlighting
*/
_getFullRouteFeature(properties) {
const routesLayer = this.controller.layerManager.getLayer('routes')
if (!routesLayer) return null
const source = this.map.getSource(routesLayer.sourceId)
if (!source) return null
// Get the source data (GeoJSON FeatureCollection)
// Try multiple ways to access the data
let sourceData = null
// Method 1: Internal _data property (most common)
if (source._data) {
sourceData = source._data
}
// Method 2: Serialize and deserialize (fallback)
else if (source.serialize) {
const serialized = source.serialize()
sourceData = serialized.data
}
// Method 3: Use cached data from layer
else if (routesLayer.data) {
sourceData = routesLayer.data
}
if (!sourceData || !sourceData.features) return null
// Find the matching feature by properties
return sourceData.features.find(feature => {
const props = feature.properties
return props.startTime === properties.startTime &&
props.endTime === properties.endTime &&
props.pointCount === properties.pointCount
})
}
/** /**
* Compare two features to see if they represent the same route * Compare two features to see if they represent the same route
*/ */
@ -257,20 +302,24 @@ export class EventHandlers {
* Handle route click * Handle route click
*/ */
handleRouteClick(e) { handleRouteClick(e) {
const feature = e.features[0] const clickedFeature = e.features[0]
const properties = feature.properties const properties = clickedFeature.properties
// Store selected route // Get the full feature from source (not the clipped tile version)
this.selectedRouteFeature = feature // Fallback to clipped feature if full feature not found
const fullFeature = this._getFullRouteFeature(properties) || clickedFeature
// Store selected route (use full feature)
this.selectedRouteFeature = fullFeature
// Update hover layer to show selected route // Update hover layer to show selected route
const routesLayer = this.controller.layerManager.getLayer('routes') const routesLayer = this.controller.layerManager.getLayer('routes')
if (routesLayer) { if (routesLayer) {
routesLayer.setHoverRoute(feature) routesLayer.setHoverRoute(fullFeature)
} }
// Create markers for selected route // Create markers for selected route
this._createRouteMarkers(feature) this._createRouteMarkers(fullFeature)
// Calculate duration // Calculate duration
const durationSeconds = properties.endTime - properties.startTime const durationSeconds = properties.endTime - properties.startTime