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
|
import wellknown from 'wellknown';
import { useEffect, useState } from 'react';
import { map } from './Map';
import { useEffectAsync } from '../reactHelper';
import { reverseCoordinates } from './mapUtil';
const GeofenceMap = () => {
const id = 'geofences';
const [geofences, setGeofences] = useState([]);
useEffectAsync(async () => {
const response = await fetch('/api/geofences');
if (response.ok) {
setGeofences(await response.json());
}
}, []);
useEffect(() => {
map.addSource(id, {
'type': 'geojson',
'data': {
type: 'FeatureCollection',
features: []
}
});
map.addLayer({
'id': id,
'type': 'fill',
'source': id,
'layout': {},
'paint': {
'fill-color': '#088',
'fill-opacity': 0.8
}
});
return () => {
map.removeLayer(id);
map.removeSource(id);
};
}, []);
useEffect(() => {
map.getSource(id).setData({
type: 'FeatureCollection',
features: geofences.map(item => [item.name, reverseCoordinates(wellknown(item.area))]).filter(([, geometry]) => !!geometry).map(([name, geometry]) => ({
type: 'Feature',
geometry: geometry,
properties: { name },
})),
});
}, [geofences]);
return null;
}
export default GeofenceMap;
|