Skip to content

Commit 2d18dbf

Browse files
committed
Add option to disable rotation
1 parent e2f2618 commit 2d18dbf

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

lib/Mapbox/Map.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class Map extends React.Component {
1010
bounds: PropTypes.array.isRequired,
1111
minZoom: PropTypes.number,
1212
maxZoom: PropTypes.number,
13+
disableRotation: PropTypes.bool,
1314

1415
onPan: PropTypes.func,
1516
onZoom: PropTypes.func,
@@ -19,6 +20,7 @@ export default class Map extends React.Component {
1920
static defaultProps = {
2021
minZoom: 3,
2122
maxZoom: 16,
23+
disableRotation: false,
2224
onPan: () => {},
2325
onZoom: () => {},
2426
onZoomEnd: () => {}
@@ -43,7 +45,7 @@ export default class Map extends React.Component {
4345
}
4446

4547
componentDidMount() {
46-
const { minZoom, maxZoom, onPan, onZoom, onZoomEnd, bounds } = this.props
48+
const { minZoom, maxZoom, disableRotation, onPan, onZoom, onZoomEnd, bounds } = this.props
4749

4850
this.map = new mapboxgl.Map({
4951
container: this.mapNodeRef.current,
@@ -54,6 +56,11 @@ export default class Map extends React.Component {
5456
attributionControl: false
5557
})
5658

59+
if (disableRotation) {
60+
this.map.dragRotate.disable()
61+
this.map.touchZoomRotate.disableRotation()
62+
}
63+
5764
this.map.boxZoom.disable()
5865
this.map.addControl(new mapboxgl.AttributionControl(), 'bottom-left')
5966

@@ -88,10 +95,21 @@ export default class Map extends React.Component {
8895
}
8996

9097
componentDidUpdate(prevProps) {
91-
const { bounds } = this.props
98+
const { bounds, disableRotation } = this.props
9299
if (!arraysAreEqual(prevProps.bounds, bounds)) {
93100
this.map.fitBounds(bounds)
94101
}
102+
103+
if (prevProps.disableRotation !== disableRotation) {
104+
if (disableRotation) {
105+
this.map.dragRotate.disable()
106+
this.map.touchZoomRotate.disableRotation()
107+
}
108+
else {
109+
this.map.dragRotate.enable()
110+
this.map.touchZoomRotate.enableRotation()
111+
}
112+
}
95113
}
96114

97115
updateStyle = () => {

0 commit comments

Comments
 (0)