Skip to content

Commit

Permalink
Merge pull request #1063 from datopian/feature/auto-zoom-in-map-compo…
Browse files Browse the repository at this point in the history
…nnet

Created auto zoom configuration for the map component
  • Loading branch information
Gutts-n authored Dec 22, 2023
2 parents 1394f02 + b13e3ad commit 8560f16
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-apples-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@portaljs/components': patch
---

Created auto zoom configuration for the map component
28 changes: 25 additions & 3 deletions packages/components/src/components/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { CSSProperties, useEffect, useState } from 'react';
import LoadingSpinner from './LoadingSpinner';
import loadData from '../lib/loadData';
import chroma from 'chroma-js';
Expand Down Expand Up @@ -30,7 +30,10 @@ export type MapProps = {
title?: string;
center?: { latitude: number | undefined; longitude: number | undefined };
zoom?: number;
style?: Object;
style?: CSSProperties;
autoZoomConfiguration?: {
layerName: string
}
};

export function Map({
Expand All @@ -45,7 +48,8 @@ export function Map({
center = { latitude: 45, longitude: 45 },
zoom = 2,
title = '',
style = {}
style = {},
autoZoomConfiguration = undefined,
}: MapProps) {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [layersData, setLayersData] = useState<any>([]);
Expand Down Expand Up @@ -118,6 +122,24 @@ export function Map({
};

if (title) info.addTo(map.target);
if(!autoZoomConfiguration) return;

let layerToZoomBounds = L.latLngBounds(L.latLng(0, 0), L.latLng(0, 0));

layers.forEach((layer) => {
if(layer.name === autoZoomConfiguration.layerName) {
const data = layersData.find(
(layerData) => layerData.name === layer.name
)?.data;

if (data) {
layerToZoomBounds = L.geoJSON(data).getBounds();
return;
}
}
});

map.target.fitBounds(layerToZoomBounds);
}}
>
<TileLayer
Expand Down
31 changes: 31 additions & 0 deletions packages/components/stories/Map.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const meta: Meta = {
},
style: {
description: "Styles for the container"
},
autoZoomConfiguration: {
description: "Configuration to auto zoom in the specified layer data"
}
},
};
Expand Down Expand Up @@ -91,4 +94,32 @@ export const GeoJSONMultipleLayers: Story = {
center: { latitude: 45, longitude: 0 },
zoom: 2,
},
}

export const GeoJSONMultipleLayersWithAutoZoomInSpecifiedLayer: Story = {
name: 'GeoJSON polygons and points map with auto zoom in the points layer',
args: {
layers: [
{
data: 'https://opendata.arcgis.com/datasets/9c58741995174fbcb017cf46c8a42f4b_25.geojson',
name: 'Points',
tooltip: true,
},
{
data: 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_geography_marine_polys.geojson',
name: 'Polygons',
tooltip: true,
colorScale: {
starting: '#ff0000',
ending: '#00ff00',
},
},
],
title: 'Polygons and points',
center: { latitude: 45, longitude: 0 },
zoom: 2,
autoZoomConfiguration: {
layerName: 'Points'
}
},
};

1 comment on commit 8560f16

@vercel
Copy link

@vercel vercel bot commented on 8560f16 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

portaljs-storybook – ./packages/components/

portaljs-storybook-datopian1.vercel.app
storybook.portaljs.org
portaljs-storybook.vercel.app
portaljs-storybook-git-main-datopian1.vercel.app

Please sign in to comment.