-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
78 lines (61 loc) · 1.79 KB
/
index.js
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
73
74
75
76
77
78
// @flow
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type MapboxNavigationControl from 'mapbox-gl/src/ui/control/navigation_control';
import MapContext from '../MapContext';
import mapboxgl from '../../utils/mapbox-gl';
type Props = {
/** If true the compass button is included. */
showCompass: boolean,
/** If true the zoom-in and zoom-out buttons are included. */
showZoom: boolean,
/**
* If true the pitch is visualized by rotating X-axis of compass
* and pitch will reset by clicking on the compass.
*/
visualizePitch: boolean,
/** A string representing the position of the control on the map. */
position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
};
/**
* A `NavigationControl` control contains zoom buttons and a compass.
*/
class NavigationControl extends PureComponent<Props> {
_map: MapboxMap;
_control: MapboxNavigationControl;
static defaultProps = {
showCompass: true,
showZoom: true,
visualizePitch: false,
position: 'top-right'
};
componentDidMount() {
const map: MapboxMap = this._map;
const { showCompass, showZoom, visualizePitch, position } = this.props;
const control: MapboxNavigationControl = new mapboxgl.NavigationControl({
showCompass,
showZoom,
visualizePitch
});
map.addControl(control, position);
this._control = control;
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle()) {
return;
}
this._map.removeControl(this._control);
}
getControl() {
return this._control;
}
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return null;
});
}
}
export default NavigationControl;