Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set max bounds for pan #442

Closed
ahmadsholehin opened this issue Jan 18, 2018 · 2 comments
Closed

Set max bounds for pan #442

ahmadsholehin opened this issue Jan 18, 2018 · 2 comments

Comments

@ahmadsholehin
Copy link

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.

@Pessimistress
Copy link
Collaborator

@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.

@ahmadsholehin
Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants