-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added multi-layer support to map popup
refactor map popup rendering logic, instead of adding event listener to each layer for popup, this commit changed to add an event listener globally and then query features of the current position Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
7 changed files
with
321 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
maps_dashboards/public/components/tooltip/create_tooltip.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Popup, MapGeoJSONFeature } from 'maplibre-gl'; | ||
|
||
import { MapLayerSpecification, OSMLayerSpecification } from '../../model/mapLayerType'; | ||
import { TooltipContainer } from './tooltipContainer'; | ||
|
||
type Options = { | ||
featureGroup: GeoJSON.Feature[][]; | ||
showCloseButton?: boolean; | ||
showPagination?: boolean; | ||
showLayerSelection?: boolean; | ||
}; | ||
|
||
export function isTooltipEnabledLayer( | ||
layer: MapLayerSpecification | ||
): layer is Exclude<MapLayerSpecification, OSMLayerSpecification> { | ||
return layer.type !== 'opensearch_vector_tile_map' && layer.source.showTooltips === true; | ||
} | ||
|
||
export function groupFeaturesByLayers( | ||
features: MapGeoJSONFeature[], | ||
layers: Exclude<MapLayerSpecification, OSMLayerSpecification>[] | ||
) { | ||
const featureGroups: MapGeoJSONFeature[][] = []; | ||
if (layers.length > 0) { | ||
layers.forEach((l) => { | ||
const layerFeatures = features.filter((f) => f.layer.source === l.id); | ||
if (layerFeatures.length > 0) { | ||
featureGroups.push(layerFeatures); | ||
} | ||
}); | ||
} else { | ||
featureGroups.push(features); | ||
} | ||
return featureGroups; | ||
} | ||
|
||
export function getPopupLngLat(geometry: GeoJSON.Geometry) { | ||
// geometry.coordinates is different for different geometry.type, here we use the geometry.coordinates | ||
// of a Point as the position of the popup. For other types, such as Polygon, MultiPolygon, etc, | ||
// use mouse position should be better | ||
if (geometry.type === 'Point') { | ||
return [geometry.coordinates[0], geometry.coordinates[1]] as [number, number]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
export function createPopup({ | ||
featureGroup, | ||
showCloseButton = true, | ||
showPagination = true, | ||
showLayerSelection = true, | ||
}: Options) { | ||
const popup = new Popup({ | ||
closeButton: false, | ||
closeOnClick: false, | ||
maxWidth: 'max-content', | ||
}); | ||
|
||
// Don't show popup if no feature | ||
if (featureGroup.length === 0) { | ||
return null; | ||
} | ||
|
||
const div = document.createElement('div'); | ||
ReactDOM.render( | ||
<TooltipContainer | ||
featureGroup={featureGroup} | ||
onClose={popup.remove} | ||
showCloseButton={showCloseButton} | ||
showPagination={showPagination} | ||
showLayerSelection={showLayerSelection} | ||
/>, | ||
div | ||
); | ||
|
||
return popup.setDOMContent(div); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.