mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-02-05 16:03:14 +00:00
fix layer filtering in event manager
This commit is contained in:
@@ -142,9 +142,9 @@ export class MapLayerEventManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _handleMouseMove(e: maplibregl.MapMouseEvent) {
|
private _handleMouseMove(e: maplibregl.MapMouseEvent) {
|
||||||
const layerIds = this._filterLayersContainingCoordinate(
|
const layerIds = this._filterLayersIntersectingBounds(
|
||||||
Object.keys(this._listeners),
|
Object.keys(this._listeners),
|
||||||
e.lngLat
|
this._getBounds(e.point)
|
||||||
);
|
);
|
||||||
const features =
|
const features =
|
||||||
layerIds.length > 0
|
layerIds.length > 0
|
||||||
@@ -228,11 +228,11 @@ export class MapLayerEventManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _handleTouchStart(e: maplibregl.MapTouchEvent) {
|
private _handleTouchStart(e: maplibregl.MapTouchEvent) {
|
||||||
const layerIds = this._filterLayersContainingCoordinate(
|
const layerIds = this._filterLayersIntersectingBounds(
|
||||||
Object.keys(this._listeners).filter(
|
Object.keys(this._listeners).filter(
|
||||||
(layerId) => this._listeners[layerId].touchstarts.length > 0
|
(layerId) => this._listeners[layerId].touchstarts.length > 0
|
||||||
),
|
),
|
||||||
e.lngLat
|
this._getBounds(e.point)
|
||||||
);
|
);
|
||||||
if (layerIds.length === 0) return;
|
if (layerIds.length === 0) return;
|
||||||
const features = this._map.queryRenderedFeatures(e.points[0], { layers: layerIds });
|
const features = this._map.queryRenderedFeatures(e.points[0], { layers: layerIds });
|
||||||
@@ -258,17 +258,28 @@ export class MapLayerEventManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _filterLayersContainingCoordinate(
|
private _getBounds(point: maplibregl.Point) {
|
||||||
|
const delta = 30;
|
||||||
|
return new maplibregl.LngLatBounds(
|
||||||
|
this._map.unproject([point.x - delta, point.y + delta]),
|
||||||
|
this._map.unproject([point.x + delta, point.y - delta])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _filterLayersIntersectingBounds(
|
||||||
layerIds: string[],
|
layerIds: string[],
|
||||||
lngLat: maplibregl.LngLat
|
bounds: maplibregl.LngLatBounds
|
||||||
): string[] {
|
): string[] {
|
||||||
let result = layerIds.filter((layerId) => {
|
let result = layerIds.filter((layerId) => {
|
||||||
if (!this._map.getLayer(layerId)) return false;
|
if (!this._map.getLayer(layerId)) return false;
|
||||||
const fileId = layerId.replace('-waypoints', '');
|
const fileId = layerId.replace('-waypoints', '');
|
||||||
if (fileId === layerId) {
|
if (fileId === layerId) {
|
||||||
return fileStateCollection.getStatistics(fileId)?.inBBox(lngLat) ?? true;
|
return fileStateCollection.getStatistics(fileId)?.intersectsBBox(bounds) ?? true;
|
||||||
} else {
|
} else {
|
||||||
return fileStateCollection.getStatistics(fileId)?.inWaypointBBox(lngLat) ?? true;
|
return (
|
||||||
|
fileStateCollection.getStatistics(fileId)?.intersectsWaypointBBox(bounds) ??
|
||||||
|
true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export class GPXStatisticsTree {
|
|||||||
return statistics;
|
return statistics;
|
||||||
}
|
}
|
||||||
|
|
||||||
inBBox(coordinates: { lat: number; lng: number }): boolean {
|
intersectsBBox(bounds: maplibregl.LngLatBounds): boolean {
|
||||||
for (let key in this.statistics) {
|
for (let key in this.statistics) {
|
||||||
const stats = this.statistics[key];
|
const stats = this.statistics[key];
|
||||||
if (stats instanceof GPXStatistics) {
|
if (stats instanceof GPXStatistics) {
|
||||||
@@ -57,18 +57,18 @@ export class GPXStatisticsTree {
|
|||||||
stats.global.bounds.southWest,
|
stats.global.bounds.southWest,
|
||||||
stats.global.bounds.northEast
|
stats.global.bounds.northEast
|
||||||
);
|
);
|
||||||
if (!bbox.isEmpty() && bbox.contains(coordinates)) {
|
if (!bbox.isEmpty() && bbox.intersects(bounds)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (stats.inBBox(coordinates)) {
|
} else if (stats.intersectsBBox(bounds)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inWaypointBBox(coordinates: { lat: number; lng: number }): boolean {
|
intersectsWaypointBBox(bounds: maplibregl.LngLatBounds): boolean {
|
||||||
return !this.wptBounds.isEmpty() && this.wptBounds.contains(coordinates);
|
return !this.wptBounds.isEmpty() && this.wptBounds.intersects(bounds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export type GPXFileWithStatistics = { file: GPXFile; statistics: GPXStatisticsTree };
|
export type GPXFileWithStatistics = { file: GPXFile; statistics: GPXStatisticsTree };
|
||||||
|
|||||||
Reference in New Issue
Block a user