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

Retrieving bounds and zoom of map #3344

Closed
SymbolixAU opened this issue Jul 11, 2019 · 5 comments
Closed

Retrieving bounds and zoom of map #3344

SymbolixAU opened this issue Jul 11, 2019 · 5 comments
Labels

Comments

@SymbolixAU
Copy link
Contributor

SymbolixAU commented Jul 11, 2019

From the onViewStateChange documentation we know that a

callback is fired when the user has interacted with the deck.gl canvas, e.g. using mouse, touch or keyboard.

So we can observe / listen for interaction and use the viewState data by putting a function inside onViewStateChange, for example

const deckgl = new deck.DeckGL({
  mapboxApiAccessToken: x,
  container: mycontainer,
  onViewStateChange: ({viewState}) => {
    console.log( viewState );
  }
});

Which gives us something like

{
    "width": 1388.0,
    "height": 400.0,
    "latitude": -2.719598,
    "longitude": -2.164757,
    "zoom": 0.2425895,
    "bearing": 0.0,
    "pitch": 0.0,
    "altitude": 1.5,
    "maxZoom": 20.0,
    "minZoom": 0.0,
    "maxPitch": 60.0,
    "minPitch": 0.0,
    "position": [
        0.0,
        0.0,
        0.0
    ],
    "transitionDuration": 0.0
}

Questions

  1. Is this the correct approach for returning and interacting with the map zoom/bearing/pitch?
  2. Is there a way or formula we can use to get the lon/lat bounds of the map, given we know the zoom, centre, pitch, bearing and the width/height of the canvas?
@Pessimistress
Copy link
Collaborator

Yes, this is the correct callback to use to listen to map state changes.

To intercept and manipulate camera changes, you can do something like:

onViewStateChange: ({viewState}) => {
  return {
    ...viewState,
    zoom: Math.max(viewState.zoom, 3)
  };
}

To get the bounds of the map:

import {WebMercatorViewport} from '@deck.gl/core';

onViewStateChange: ({viewState}) => {
  const viewport = new WebMercatorViewport(viewState);

  const nw = viewport.unproject([0, 0]);
  const se = viewport.unproject([viewport.width, viewport.height]);

  // do what you need to do
}

@SymbolixAU
Copy link
Contributor Author

Thanks, I'll give it a go!

@tgorkin tgorkin closed this as completed Aug 6, 2019
@SymbolixAU
Copy link
Contributor Author

SymbolixAU commented Sep 20, 2019

@Pessimistress The documentation for unproject says

Unproject pixel coordinates on screen onto [lng, lat, altitude] on map

However, using unproject as per your suggestion, the longitude values are not restricted to (-180, 180)

Reproducible steps, log the state change

onViewStateChange: ({viewState}) => {
  const viewport = new WebMercatorViewport(viewState);
  const nw = viewport.unproject([0, 0]);
  const se = viewport.unproject([viewport.width, viewport.height]);
  console.log("north: ", nw[1], ", south: ", se[1]);
  console.log("east: ", se[0], "west: ", nw[0] );
});

and scroll the map east/west

Screen Shot 2019-09-20 at 11 37 31 am

I think another adjustment is required in unproject to get the longitude correctly into (-180, 180)?

@SymbolixAU
Copy link
Contributor Author

@tgorkin do you think it's worth reopening this?

@Jamal8548
Copy link

You will want to manage view state in your react app with deck's viewState and onViewStateChange prop. People usually hook this up to a useState hook.

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

No branches or pull requests

3 participants