-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.tsx
55 lines (50 loc) · 1.54 KB
/
Map.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useContext, useMemo } from "react";
import { MapContainer, Marker, Popup, TileLayer, useMap } from "react-leaflet";
import { useWindowSize } from "../hooks/useWindowSize";
import LocationContext from "../contexts/locationContext";
function ChangeView({ center, zoom }) {
const map = useMap();
map.setView(center, zoom);
return null;
}
const Map: React.FC = () => {
const { height } = useWindowSize();
const { location } = useContext(LocationContext);
// this needs to be in its own place because a lot of react-leaflet props are immutable - e.g. the marker position
// it's hacky but it works
const container = useMemo(
() => (
<MapContainer
center={[location.lat, location.lng]}
zoom={13}
scrollWheelZoom={false}
markerZoomAnimation={true}
className="map"
>
<ChangeView center={[location.lat, location.lng]} zoom={13} />
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[location.lat, location.lng]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
),
[location]
);
return (
<>
<style>
{`
.map {
height: ${height - 250 /* screen height - header height (ie "rest of the page") */}px !important;
}`}
</style>
{container}
</>
);
};
export default Map;