-
Notifications
You must be signed in to change notification settings - Fork 15
/
OverlayFeatureLayers.tsx
72 lines (67 loc) · 1.99 KB
/
OverlayFeatureLayers.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Feature, FeatureCollection } from "geojson";
import { Icon, Marker } from "leaflet";
import ReactDOMServer from "react-dom/server";
import { GeoJSON, LayersControl } from "react-leaflet";
type Props = {
featureCollections: {
[name: string]: FeatureCollection;
};
};
const iconColors = [
"blue",
"gold",
"red",
"green",
"orange",
"yellow",
"violet",
"gray",
"black",
];
function FeaturePopup({ feature }: { feature: Feature }) {
return (
<>
<h2 className="h6 text-center">{feature.properties?.name}</h2>
<ul className="list-unstyled">
{feature.properties &&
Object.entries(feature.properties)
.filter(([key, _]) => key !== "name")
.map(([key, val]) => (
<li>
<b>{key}</b>: {val}
</li>
))}
</ul>
</>
);
}
export function OverlayFeatureLayers({ featureCollections }: Props) {
return (
<>
{Object.entries(featureCollections).map(
([name, featureCollection], i) => {
const iconColor = iconColors[i % iconColors.length];
const icon = new Icon.Default({
iconUrl: `https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-${iconColor}.png`,
iconRetinaUrl: `https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-${iconColor}.png`,
});
return (
<LayersControl.Overlay key={name} name={name}>
<GeoJSON
data={featureCollection}
pointToLayer={(pointFeature, latlng) => {
return new Marker(latlng, { icon: icon }).bindPopup(
// https://stackoverflow.com/a/60686195
ReactDOMServer.renderToString(
<FeaturePopup feature={pointFeature} />
)
);
}}
/>
</LayersControl.Overlay>
);
}
)}
</>
);
}