From b515c626aa45bcb00fd5c1fdff356f7d66dd01de Mon Sep 17 00:00:00 2001 From: Sujit Date: Wed, 31 Jul 2024 17:55:19 +0545 Subject: [PATCH 1/7] feat: add image loading feature on `vectorLayer` component --- .../MapLibreComponents/Layers/VectorLayer.ts | 22 +++++++++++++++++++ .../common/MapLibreComponents/types/index.ts | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/frontend/src/components/common/MapLibreComponents/Layers/VectorLayer.ts b/src/frontend/src/components/common/MapLibreComponents/Layers/VectorLayer.ts index d752c670..f1fd6b65 100644 --- a/src/frontend/src/components/common/MapLibreComponents/Layers/VectorLayer.ts +++ b/src/frontend/src/components/common/MapLibreComponents/Layers/VectorLayer.ts @@ -1,6 +1,7 @@ /* eslint-disable no-param-reassign */ import { useEffect, useMemo, useRef } from 'react'; import { MapMouseEvent } from 'maplibre-gl'; +import { v4 as uuidv4 } from 'uuid'; import { IVectorLayer } from '../types'; export default function VectorLayer({ @@ -12,6 +13,8 @@ export default function VectorLayer({ layerOptions, onFeatureSelect, visibleOnMap = true, + hasImage = false, + image, }: IVectorLayer) { const sourceId = useMemo(() => id.toString(), [id]); const hasInteractions = useRef(false); @@ -42,6 +45,25 @@ export default function VectorLayer({ layout: {}, ...layerOptions, }); + + if (hasImage) { + const imageId = uuidv4(); + map.loadImage(image, (error, img: any) => { + if (error) throw error; + // Add the loaded image to the style's sprite with the ID 'kitten'. + map.addImage(imageId, img); + }); + map.addLayer({ + id: imageId, + type: 'symbol', + source: sourceId, + layout: { + 'icon-image': imageId, + 'icon-size': 1, + 'icon-overlap': 'always', + }, + }); + } } else if (map.getLayer(sourceId)) { map.removeLayer(sourceId); } diff --git a/src/frontend/src/components/common/MapLibreComponents/types/index.ts b/src/frontend/src/components/common/MapLibreComponents/types/index.ts index 4643b68e..374614f1 100644 --- a/src/frontend/src/components/common/MapLibreComponents/types/index.ts +++ b/src/frontend/src/components/common/MapLibreComponents/types/index.ts @@ -45,6 +45,8 @@ export interface IVectorLayer extends ILayer { geojson: GeojsonType | null; interactions?: string[]; onFeatureSelect?: (properties: Record) => void; + hasImage?: boolean; + image: any; } type InteractionsType = 'hover' | 'select'; From 9240136ee286cfa244b342b11513ce48e9439b52 Mon Sep 17 00:00:00 2001 From: Sujit Date: Wed, 31 Jul 2024 18:02:17 +0545 Subject: [PATCH 2/7] fix: map loading state delayed issue set loading state to true on `load` event of map instance insted of map instance saved on state --- .../common/MapLibreComponents/useMapLibreGLMap/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/frontend/src/components/common/MapLibreComponents/useMapLibreGLMap/index.ts b/src/frontend/src/components/common/MapLibreComponents/useMapLibreGLMap/index.ts index 54ae8a1b..481faf67 100644 --- a/src/frontend/src/components/common/MapLibreComponents/useMapLibreGLMap/index.ts +++ b/src/frontend/src/components/common/MapLibreComponents/useMapLibreGLMap/index.ts @@ -22,6 +22,10 @@ export default function useMapLibreGLMap({ ...mapOptions, }); setMap(mapInstance); + + mapInstance.on('load', () => { + setIsMapLoaded(true); + }); // return () => mapInstance.setTarget(undefined); }, []); // eslint-disable-line From 158cedcf52f0b57b08a7838f13a9581681f12cb2 Mon Sep 17 00:00:00 2001 From: Sujit Date: Wed, 31 Jul 2024 22:54:29 +0545 Subject: [PATCH 3/7] feat: add button hide props on map popup component --- .../common/MapLibreComponents/AsyncPopup/index.tsx | 8 ++++++-- .../components/common/MapLibreComponents/types/index.ts | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/frontend/src/components/common/MapLibreComponents/AsyncPopup/index.tsx b/src/frontend/src/components/common/MapLibreComponents/AsyncPopup/index.tsx index e51aea64..e4e464da 100644 --- a/src/frontend/src/components/common/MapLibreComponents/AsyncPopup/index.tsx +++ b/src/frontend/src/components/common/MapLibreComponents/AsyncPopup/index.tsx @@ -24,6 +24,7 @@ export default function AsyncPopup({ isLoading = false, onClose, buttonText = 'View More', + hideButton = false, }: IAsyncPopup) { const [properties, setProperties] = useState | null>( null, @@ -68,7 +69,10 @@ export default function AsyncPopup({ if (!properties) return
; return ( -
+
{isLoading ? ( @@ -86,7 +90,7 @@ export default function AsyncPopup({
- {!isLoading && ( + {!isLoading && !hideButton && (