2021-05-21 17:52:56 +00:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from "react"
|
2021-05-18 14:53:42 +00:00
|
|
|
import Style from "./BoxMap.module.css"
|
|
|
|
import BoxFull from "./BoxFull"
|
|
|
|
import { MapContainer, TileLayer } from "react-leaflet"
|
|
|
|
|
|
|
|
|
2021-05-21 17:52:56 +00:00
|
|
|
export default function BoxMap(
|
|
|
|
{
|
|
|
|
mapViewHook,
|
|
|
|
button,
|
|
|
|
children,
|
|
|
|
...props
|
|
|
|
}) {
|
|
|
|
const [map, setMap] = useState(null)
|
|
|
|
|
|
|
|
const onMapMove = useCallback(
|
|
|
|
() => mapViewHook.setCenter(map.getCenter()),
|
|
|
|
[mapViewHook, map],
|
|
|
|
)
|
|
|
|
|
|
|
|
const onMapZoom = useCallback(
|
|
|
|
() => mapViewHook.setZoom(map.getZoom()),
|
|
|
|
[mapViewHook, map],
|
|
|
|
)
|
|
|
|
|
|
|
|
const mapContainer = useMemo(
|
|
|
|
() => (
|
2021-05-18 14:53:42 +00:00
|
|
|
<MapContainer
|
2021-05-21 17:52:56 +00:00
|
|
|
center={[mapViewHook.center.lat, mapViewHook.center.lng]}
|
|
|
|
zoom={mapViewHook.zoom}
|
2021-05-18 14:53:42 +00:00
|
|
|
className={Style.MapContainer}
|
|
|
|
whenCreated={setMap}
|
|
|
|
>
|
|
|
|
<TileLayer
|
|
|
|
attribution='(c) <a href="https://osm.org/copyright">OpenStreetMap contributors</a>'
|
|
|
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
|
|
/>
|
2021-05-20 09:39:40 +00:00
|
|
|
{children}
|
2021-05-18 14:53:42 +00:00
|
|
|
<div className={"leaflet-top leaflet-right"}>
|
|
|
|
<div className={"leaflet-control"}>
|
|
|
|
{button}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</MapContainer>
|
2021-05-21 17:52:56 +00:00
|
|
|
),
|
|
|
|
[mapViewHook],
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if(map === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
map.on("move", onMapMove)
|
|
|
|
map.on("zoom", onMapZoom)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
map.off("move", onMapMove)
|
|
|
|
map.off("zoom", onMapZoom)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[map, mapViewHook]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BoxFull
|
|
|
|
childrenClassName={Style.BoxMapContents}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{mapContainer}
|
2021-05-18 14:53:42 +00:00
|
|
|
</BoxFull>
|
|
|
|
)
|
|
|
|
}
|