React-mapbox-gl: Feature Request - getBounds()

Created on 24 Nov 2017  路  5Comments  路  Source: alex3165/react-mapbox-gl

Hi,

I have seen getBounds() in the mapboxgl js docs and it would be so useful to have this exposed at the React level.

At the moment I am getting the MapboxGl.Map object manually and using the getBounds() on that, but I don't like bypassing the react.

Most helpful comment

When do you need the bounds? If you need the bounds on map move you could do something like this:

<ReactMapboxGl
  onMove={(map) => { const bounds = map.getBounds()}}
/>

All 5 comments

Hey @iambalaam, how would you see the API for that?

I've only seen that the ReactMapboxGlcomponent has the map in its state, and it passes this down to all child components in their context. You can call getBounds() on this. I have only been doing this manually though, and haven't managed to get this to work in my code.

Would it be possible to pass in a custom child component to access this context?

When do you need the bounds? If you need the bounds on map move you could do something like this:

<ReactMapboxGl
  onMove={(map) => { const bounds = map.getBounds()}}
/>

Ah! apologies, I didn't realise these events exposed the whole map object.

I want to call an api to get features to pass in a GeoJSONLayer to the map. So this helps when the map is in use, but what would be the best way to initialise the features. I've now looked closer in your docs at this, am I correct in assuming that onStyleLoad will be the first to fire?

yes that's the one you should use

Was this page helpful?
0 / 5 - 0 ratings