File tree Expand file tree Collapse file tree 5 files changed +86
-1
lines changed
components/NavigationControl Expand file tree Collapse file tree 5 files changed +86
-1
lines changed Original file line number Diff line number Diff line change 1+ A ` NavigationControl ` control contains zoom buttons and a compass.
2+
3+ ``` jsx
4+ < MapGL
5+ style= {{ width: ' 100%' , height: ' 400px' }}
6+ mapStyle= ' mapbox://styles/mapbox/light-v9'
7+ accessToken= {MAPBOX_ACCESS_TOKEN }
8+ latitude= {37.78 }
9+ longitude= {- 122.41 }
10+ zoom= {11 }
11+ >
12+ < NavigationControl showCompass showZoom position= ' top-left' / >
13+ < / MapGL>
14+ ```
Original file line number Diff line number Diff line change 33import type Map from 'mapbox-gl/src/ui/map' ;
44import type Popup from 'mapbox-gl/src/ui/popup' ;
55import type Marker from 'mapbox-gl/src/ui/marker' ;
6+ import type NavigationControl from 'mapbox-gl/src/ui/control/navigation_control' ;
67import type { Map as ImmutableMap } from 'immutable' ;
78import type { MapMouseEvent , MapTouchEvent } from 'mapbox-gl/src/ui/events' ;
89
@@ -20,6 +21,8 @@ declare type MapboxPopup = Popup;
2021
2122declare type MapboxMarker = Marker ;
2223
24+ declare type MapboxNavigationControl = NavigationControl ;
25+
2326declare type MapboxLngLatBoundsLike = LngLatBoundsLike ;
2427
2528declare type MapboxLayerSpecification = LayerSpecification ;
Original file line number Diff line number Diff line change 1+ // @flow
2+
3+ import { PureComponent , createElement } from 'react' ;
4+
5+ import MapContext from '../MapContext' ;
6+ import mapboxgl from '../../utils/mapbox-gl' ;
7+
8+ type Props = {
9+ /* If true the compass button is included. */
10+ showCompass : boolean ,
11+
12+ /* If true the zoom-in and zoom-out buttons are included. */
13+ showZoom : boolean ,
14+
15+ /* A string representing the position of the control on the map. */
16+ position ?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
17+ } ;
18+
19+ /**
20+ * A `NavigationControl` control contains zoom buttons and a compass.
21+ */
22+ class NavigationControl extends PureComponent < Props > {
23+ _map : MapboxMap ;
24+
25+ _control : MapboxNavigationControl ;
26+
27+ static defaultProps = {
28+ position : 'top-right'
29+ } ;
30+
31+ componentDidMount ( ) {
32+ const map : MapboxMap = this . _map ;
33+ const { showCompass, showZoom, position } = this . props ;
34+
35+ const control : MapboxNavigationControl = new mapboxgl . NavigationControl ( {
36+ showCompass,
37+ showZoom
38+ } ) ;
39+
40+ map . addControl ( control , position ) ;
41+ this . _control = control ;
42+ }
43+
44+ componentWillUnmount ( ) {
45+ if ( ! this . _map ) {
46+ return ;
47+ }
48+
49+ this . _map . removeControl ( this . _control ) ;
50+ }
51+
52+ render ( ) {
53+ return createElement ( MapContext . Consumer , { } , ( map ) => {
54+ if ( map ) {
55+ this . _map = map ;
56+ }
57+ return null ;
58+ } ) ;
59+ }
60+ }
61+
62+ export default NavigationControl ;
Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ export { default as Source } from './components/Source';
66export { default as Popup } from './components/Popup' ;
77export { default as Marker } from './components/Marker' ;
88export { default as Cluster } from './components/Cluster' ;
9+ export { default as NavigationControl } from './components/NavigationControl' ;
Original file line number Diff line number Diff line change @@ -23,7 +23,8 @@ module.exports = {
2323 'src/components/Layer/index.js' ,
2424 'src/components/Popup/index.js' ,
2525 'src/components/Marker/index.js' ,
26- 'src/components/Cluster/index.js'
26+ 'src/components/Cluster/index.js' ,
27+ 'src/components/NavigationControl/index.js'
2728 ]
2829 } ,
2930 {
@@ -80,6 +81,10 @@ module.exports = {
8081 {
8182 name : 'Change Map style' ,
8283 content : 'docs/change-map-style.md'
84+ } ,
85+ {
86+ name : 'Navigation Control' ,
87+ content : 'docs/navigation-control.md'
8388 }
8489 ]
8590 }
You can’t perform that action at this time.
0 commit comments