Skip to content

Commit

Permalink
add minZoom and maxZoom
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Sep 19, 2018
1 parent 8b3418d commit 1050e14
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -127,6 +127,10 @@ If you're interested in making a new plugin, check out the code of [pigeon-marke

**zoomSnap** - Snap to discrete zoom increments (14, 15, 16, etc) when scrolling with the mouse or pinching with touch events, Defaults to `true`.

**minZoom** - The lowest zoom level possible. Defaults to `1`

**maxZoom** - The highest zoom level possible. Defaults to `18`

**attribution** - What to show as an [attribution](https://www.openstreetmap.org/copyright). React node or `false` to hide.

**attributionPrefix** - Prefix before attribution. React node or `false` to hide.
Expand Down
13 changes: 11 additions & 2 deletions demo/demo.js
Expand Up @@ -86,7 +86,9 @@ export default class App extends Component {
animating: false,
zoomSnap: true,
mouseEvents: true,
touchEvents: true
touchEvents: true,
minZoom: 1,
maxZoom: 18
}
}

Expand Down Expand Up @@ -126,7 +128,7 @@ export default class App extends Component {
}

render () {
const { center, zoom, provider, animate, metaWheelZoom, twoFingerDrag, zoomSnap, mouseEvents, touchEvents, animating } = this.state
const { center, zoom, provider, animate, metaWheelZoom, twoFingerDrag, zoomSnap, mouseEvents, touchEvents, animating, minZoom, maxZoom } = this.state

return (
<div style={{textAlign: 'center', marginTop: 50}}>
Expand All @@ -146,6 +148,8 @@ export default class App extends Component {
zoomSnap={zoomSnap}
mouseEvents={mouseEvents}
touchEvents={touchEvents}
minZoom={minZoom}
maxZoom={maxZoom}
attribution={
isMapBox(provider)
? <MapboxAttribution />
Expand Down Expand Up @@ -192,6 +196,11 @@ export default class App extends Component {
<button onClick={() => this.setState({ mouseEvents: !mouseEvents })}>{mouseEvents ? '[X] mouse events' : '[ ] mouse events'}</button>
<button onClick={() => this.setState({ touchEvents: !touchEvents })}>{touchEvents ? '[X] touch events' : '[ ] touch events'}</button>
</div>
<div style={{marginTop: 20}}>
minZoom: <input onChange={(e) => this.setState({ minZoom: parseInt(e.target.value) || 1 })} value={minZoom} type='number' style={{ width: 40 }} />
{' '}
maxZoom: <input onChange={(e) => this.setState({ maxZoom: parseInt(e.target.value) || 18 })} value={maxZoom} type='number' style={{ width: 40 }} />
</div>
<div style={{marginTop: 20}}>
{Object.keys(markers).map(key => (
<button key={key} onClick={() => this.setState({ center: markers[key][0], zoom: markers[key][1] })}>{key}</button>
Expand Down
22 changes: 14 additions & 8 deletions src/index.js
Expand Up @@ -77,6 +77,9 @@ export default class Map extends Component {
animate: PropTypes.bool,
animateMaxScreens: PropTypes.number,

minZoom: PropTypes.number,
maxZoom: PropTypes.number,

metaWheelZoom: PropTypes.bool,
metaWheelZoomWarning: PropTypes.string,
twoFingerDrag: PropTypes.bool,
Expand Down Expand Up @@ -106,7 +109,9 @@ export default class Map extends Component {
mouseEvents: true,
touchEvents: true,
warningZIndex: 100,
animateMaxScreens: 5
animateMaxScreens: 5,
minZoom: 1,
maxZoom: 18
}

constructor (props) {
Expand Down Expand Up @@ -453,7 +458,7 @@ export default class Map extends Component {
if (this._lastTap && performanceNow() - this._lastTap < DOUBLE_CLICK_DELAY) {
event.preventDefault()
const latLngNow = this.pixelToLatLng(this._touchStartPixel[0])
this.setCenterZoomTarget(null, Math.max(1, Math.min(this.state.zoom + 1, 18)), false, latLngNow)
this.setCenterZoomTarget(null, Math.max(this.props.minZoom, Math.min(this.state.zoom + 1, this.props.maxZoom)), false, latLngNow)
} else {
this._lastTap = performanceNow()
this.startTrackingMoveEvents(pixel)
Expand Down Expand Up @@ -521,7 +526,7 @@ export default class Map extends Component {

const distance = Math.sqrt(Math.pow(t1[0] - t2[0], 2) + Math.pow(t1[1] - t2[1], 2))

const zoomDelta = Math.min(18, zoom + Math.log2(distance / this._touchStartDistance)) - zoom
const zoomDelta = Math.max(this.props.minZoom, Math.min(this.props.maxZoom, zoom + Math.log2(distance / this._touchStartDistance))) - zoom
const scale = Math.pow(2, zoomDelta)

const centerDiffDiff = [
Expand All @@ -541,7 +546,7 @@ export default class Map extends Component {

handleTouchEnd = (event) => {
if (this._touchStartPixel) {
const { zoomSnap, twoFingerDrag } = this.props
const { zoomSnap, twoFingerDrag, minZoom, maxZoom } = this.props
const { zoomDelta } = this.state
const { center, zoom } = this.sendDeltaChange()

Expand Down Expand Up @@ -589,7 +594,7 @@ export default class Map extends Component {
} else {
zoomTarget = zoomDelta > 0 ? Math.ceil(this.state.zoom) : Math.floor(this.state.zoom)
}
const zoom = Math.max(1, Math.min(zoomTarget, 18))
const zoom = Math.max(minZoom, Math.min(zoomTarget, maxZoom))

this.setCenterZoomTarget(latLng, zoom, false, latLng)
}
Expand All @@ -608,7 +613,7 @@ export default class Map extends Component {

if (this._lastClick && performanceNow() - this._lastClick < DOUBLE_CLICK_DELAY) {
const latLngNow = this.pixelToLatLng(this._mousePosition)
this.setCenterZoomTarget(null, Math.max(1, Math.min(this.state.zoom + 1, 18)), false, latLngNow)
this.setCenterZoomTarget(null, Math.max(this.props.minZoom, Math.min(this.state.zoom + 1, this.props.maxZoom)), false, latLngNow)
} else {
this._lastClick = performanceNow()

Expand Down Expand Up @@ -800,8 +805,9 @@ export default class Map extends Component {

zoomAroundMouse = (zoomDiff, zoomSnap = false) => {
const { zoom } = this.state
const { minZoom, maxZoom } = this.props

if (!this._mousePosition || (zoom === 1 && zoomDiff < 0) || (zoom === 18 && zoomDiff > 0)) {
if (!this._mousePosition || (zoom === minZoom && zoomDiff < 0) || (zoom === maxZoom && zoomDiff > 0)) {
return
}

Expand All @@ -811,7 +817,7 @@ export default class Map extends Component {
if (zoomSnap) {
zoomTarget = zoomDiff < 0 ? Math.floor(zoomTarget) : Math.ceil(zoomTarget)
}
zoomTarget = Math.max(1, Math.min(zoomTarget, 18))
zoomTarget = Math.max(minZoom, Math.min(zoomTarget, maxZoom))

this.setCenterZoomTarget(null, zoomTarget, false, latLngNow)
}
Expand Down

0 comments on commit 1050e14

Please sign in to comment.