-
-
Save andion/aac656ff6019f0073c396f158383043c to your computer and use it in GitHub Desktop.
HeritageMap.js
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
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