React-map-gl: Set max bounds for pan

Created on 18 Jan 2018  路  2Comments  路  Source: visgl/react-map-gl

Is there an easy way to set the max bounds of the map when panning?

Tried

<MapGL
        { ...viewport }
        onViewportChange={ this._onViewportChange.bind( this ) }
        maxBounds={ [ [ ..., ... ], [ ..., ... ] ] }
>
...
</MapGL>

as well as

mapRef.getMap().setMaxBounds( [ [ ..., ... ], [ ..., ... ] ] );

Also tried to hook onto a custom map control via _onPan

_onPan( event ) {
   const viewport = this.getMapState()._viewportProps;

   // only if within bounds
   if ( ... ) {
       this._onPanMove( event );
   }
}

but that'll get everything stuck once the bounds have been reached.

Most helpful comment

Thanks @Pessimistress. For anyone looking out for the same, got it working via _onViewportChange callback:

_onViewportChange( viewport ) {
    if ( viewport.longitude < MyOverlay.maxBounds.minLongitude ) {
      viewport.longitude = MyOverlay.maxBounds.minLongitude;
    }
    else if ( viewport.longitude > MyOverlay.maxBounds.maxLongitude ) {
      viewport.longitude = MyOverlay.maxBounds.maxLongitude;
    }
    else if ( viewport.latitude < MyOverlay.maxBounds.minLatitude ) {
      viewport.latitude = MyOverlay.maxBounds.minLatitude;
    }
    else if ( viewport.latitude > MyOverlay.maxBounds.maxLatitude ) {
      viewport.latitude = MyOverlay.maxBounds.maxLatitude;
    }
    this.setState( {
      viewport: { ...this.state.viewport, ...viewport }
    } );
  }

Define a static maxBounds getter in MyOverlay (your overlay class).

All 2 comments

@ahmadsholehin The easiest way to handle this is in the _onViewportChange callback by clamping latitude and longitude, e.g.

_onViewportChange(viewport) {
  if (viewport.longitude > 0) {
    viewport.longitude = 0;
  }
  this.setState(viewport);
}

Your custom control doesn't work because the viewport props is already out of bounds, and by aborting you never update the viewport to be valid again.

Thanks @Pessimistress. For anyone looking out for the same, got it working via _onViewportChange callback:

_onViewportChange( viewport ) {
    if ( viewport.longitude < MyOverlay.maxBounds.minLongitude ) {
      viewport.longitude = MyOverlay.maxBounds.minLongitude;
    }
    else if ( viewport.longitude > MyOverlay.maxBounds.maxLongitude ) {
      viewport.longitude = MyOverlay.maxBounds.maxLongitude;
    }
    else if ( viewport.latitude < MyOverlay.maxBounds.minLatitude ) {
      viewport.latitude = MyOverlay.maxBounds.minLatitude;
    }
    else if ( viewport.latitude > MyOverlay.maxBounds.maxLatitude ) {
      viewport.latitude = MyOverlay.maxBounds.maxLatitude;
    }
    this.setState( {
      viewport: { ...this.state.viewport, ...viewport }
    } );
  }

Define a static maxBounds getter in MyOverlay (your overlay class).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huaying picture huaying  路  5Comments

cjmyles picture cjmyles  路  3Comments

AriLFrankel picture AriLFrankel  路  3Comments

Majaspic picture Majaspic  路  4Comments

SethHamilton picture SethHamilton  路  3Comments