-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(NavigationControl): add NavigationControl #116
- Loading branch information
1 parent
78e0ff7
commit 34ae92f
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
A `NavigationControl` control contains zoom buttons and a compass. | ||
|
||
```jsx | ||
<MapGL | ||
style={{ width: '100%', height: '400px' }} | ||
mapStyle='mapbox://styles/mapbox/light-v9' | ||
accessToken={MAPBOX_ACCESS_TOKEN} | ||
latitude={37.78} | ||
longitude={-122.41} | ||
zoom={11} | ||
> | ||
<NavigationControl showCompass showZoom position='top-left' /> | ||
</MapGL> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// @flow | ||
|
||
import { PureComponent, createElement } from 'react'; | ||
|
||
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, | ||
|
||
/* 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 = { | ||
position: 'top-right' | ||
}; | ||
|
||
componentDidMount() { | ||
const map: MapboxMap = this._map; | ||
const { showCompass, showZoom, position } = this.props; | ||
|
||
const control: MapboxNavigationControl = new mapboxgl.NavigationControl({ | ||
showCompass, | ||
showZoom | ||
}); | ||
|
||
map.addControl(control, position); | ||
this._control = control; | ||
} | ||
|
||
componentWillUnmount() { | ||
if (!this._map) { | ||
return; | ||
} | ||
|
||
this._map.removeControl(this._control); | ||
} | ||
|
||
render() { | ||
return createElement(MapContext.Consumer, {}, (map) => { | ||
if (map) { | ||
this._map = map; | ||
} | ||
return null; | ||
}); | ||
} | ||
} | ||
|
||
export default NavigationControl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters