-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
70 lines (54 loc) · 1.54 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
// @flow
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type MapboxScaleControl from 'mapbox-gl/src/ui/control/scale_control';
import MapContext from '../MapContext';
import mapboxgl from '../../utils/mapbox-gl';
type Props = {
/* The maximum length of the scale control in pixels. */
maxWidth: number,
/* Unit of the distance. */
unit: 'imperial' | 'metric' | 'nautical',
/* A string representing the position of the control on the map. */
position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
};
/**
* A `ScaleControl` control displays the ratio of a distance on the map
* to the corresponding distance on the ground.
*/
class ScaleControl extends PureComponent<Props> {
_map: MapboxMap;
_control: MapboxScaleControl;
static defaultProps = {
position: 'bottom-right',
unit: 'metric'
};
componentDidMount() {
const map: MapboxMap = this._map;
const { maxWidth, unit, position } = this.props;
const control: MapboxScaleControl = new mapboxgl.ScaleControl({
maxWidth,
unit
});
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 ScaleControl;