Skip to content

Instantly share code, notes, and snippets.

@andion
Last active September 18, 2020 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andion/aac656ff6019f0073c396f158383043c to your computer and use it in GitHub Desktop.
Save andion/aac656ff6019f0073c396f158383043c to your computer and use it in GitHub Desktop.
HeritageMap.js
import React, { useState, useMemo } from "react";
import { Marker } from "react-leaflet";
import { latLngBounds } from "leaflet";
import MyMap from "./components/my-map";
import HeritageLegend from "./components/heritage-legend";
const HeritageMap = ({ sites }) => {
const [selectedSite, setSelectedSite] = useState(null);
const allSiteBounds = useMemo(() => {
const bounds = latLngBounds();
sites.forEach((site) => bounds.extend(site.position));
return bounds.pad(0.1);
}, [sites]);
const handleSiteClick = (site) => () => setSelectedSite(site);
return (
<MyMap
bounds={!selectedSite && allSiteBounds}
center={selectedSite && selectedSite.position}
zoom={14}
>
{sites.map((site) => (
<Marker
key={site.position}
position={site.position}
onClick={handleSiteClick(site)}
/>
))}
<HeritageLegend
title={selectedSite && selectedSite.name}
description={selectedSite && selectedSite.description}
/>
</MyMap>
);
};
HeritageMap.propTypes = {
sites: PropTypes.arrayOf(
PropTypes.shape({
position: PropTypes.arrayOf(PropTypes.number),
name: PropTypes.string.isRequired,
description: PropTypes.string,
})
).isRequired,
};
export default HeritageMap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment