-
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 (#140)
* feat: added multi-layer support to map popup 1. Abstract a reusable popup rendering logic. 2. For click-popups which need to display multi-layers information, added an event listener globally and then query features from all layers at the current position. 3. For hover-popups which show information of the current layer features, moved the popup rendering logic out from the layer render logic(in the layer control panel component). Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
7 changed files
with
343 additions
and
157 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
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Popup, MapGeoJSONFeature } from 'maplibre-gl'; | ||
|
||
import { MapLayerSpecification, DocumentLayerSpecification } from '../../model/mapLayerType'; | ||
import { FeatureGroupItem, TooltipContainer } from './tooltipContainer'; | ||
|
||
type Options = { | ||
features: MapGeoJSONFeature[]; | ||
layers: DocumentLayerSpecification[]; | ||
showCloseButton?: boolean; | ||
showPagination?: boolean; | ||
showLayerSelection?: boolean; | ||
}; | ||
|
||
export function isTooltipEnabledLayer( | ||
layer: MapLayerSpecification | ||
): layer is DocumentLayerSpecification { | ||
return layer.type !== 'opensearch_vector_tile_map' && layer.source.showTooltips === true; | ||
} | ||
|
||
export function groupFeaturesByLayers( | ||
features: MapGeoJSONFeature[], | ||
layers: DocumentLayerSpecification[] | ||
) { | ||
const featureGroups: FeatureGroupItem[] = []; | ||
if (layers.length > 0) { | ||
layers.forEach((layer) => { | ||
const layerFeatures = features.filter((f) => f.layer.source === layer.id); | ||
if (layerFeatures.length > 0) { | ||
featureGroups.push({ features: layerFeatures, layer }); | ||
} | ||
}); | ||
} | ||
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({ | ||
features, | ||
layers, | ||
showCloseButton = true, | ||
showPagination = true, | ||
showLayerSelection = true, | ||
}: Options) { | ||
const popup = new Popup({ | ||
closeButton: false, | ||
closeOnClick: false, | ||
maxWidth: 'max-content', | ||
}); | ||
|
||
const featureGroup = groupFeaturesByLayers(features, layers); | ||
|
||
// 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.