forked from toggle-corp/re-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapBounds.tsx
50 lines (40 loc) · 1.07 KB
/
MapBounds.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
import { useContext, useEffect, useState } from 'react';
import { MapChildContext } from './context';
interface Props {
bounds?: [number, number, number, number];
padding: number;
duration: number;
}
const MapBounds = (props: Props) => {
const { map } = useContext(MapChildContext);
const {
padding,
duration,
bounds,
} = props;
const [initialPadding] = useState(padding);
const [initialDuration] = useState(duration);
// Handle change in bounds
useEffect(
() => {
if (!map || !bounds) {
return;
}
const [fooLon, fooLat, barLon, barLat] = bounds;
map.fitBounds(
[[fooLon, fooLat], [barLon, barLat]],
{
padding: initialPadding,
duration: initialDuration,
},
);
},
[map, bounds, initialPadding, initialDuration],
);
return null;
};
MapBounds.defaultProps = {
padding: 0,
duration: 200, // ms
};
export default MapBounds;