Support for initial zoom/center
Correct me if I'm wrong, but I believe right now there's no way to set initial values for zoom or center; You either need to make them controlled values, or stick with the current default.
For a project I need a specific zoom/center on load, but I otherwise don't care about zooming/panning, so I'd just like to update the initial values set to Mapbox.
Let me know if this is a feature that you'd be interested in, I can submit a PR if I don't hit any weird issues.
Hey @joshwcomeau, I don't really see what you mean here, if you pass down a zoom and center value to the Map it should work :
<ReactMapboxGl
center={[0.5375194, 50.8437787]}
zoom={[11]}
>
Tested with the london-cycle example and it works as expected.
Hey @alex3165,
Thanks for the quick reply!
So, my issue with that is that whenever the component re-renders, the map will zoom/pan to that location. I need a map that initializes at a certain position, but then is free to be panned/zoomed, with re-renders not overwriting wherever the user has left the map.
EDIT: Thought I'd add, my current workaround was to have onZoom/onMove handlers to keep the newest position in state, that way whenever they zoom/pan, that value is updated. The problem is that it's a little buggy, if the map re-renders while they're in the middle of a zoom.
Have you tried to not break the reference equality between 2 re-render, you can keep the reference by having the center and the zoom in your state like this :
state = {
center: [0.5375194, 50.8437787],
zoom: [11]
}
...
<ReactMapboxGl
center={this.state.center}
zoom={this.state.zoom}
>
Ohhh. Right, that makes total sense.
Sorry to have disturbed you! Happy to learn that it was my mistake :)
No problem happy to solve your problem 👍
But if you update the state using setState the reference still the same right? So the center will not move
Nope, you will break the reference equality if you assign a new array :
this.setState({
zoom: [new_zoom_value]
});
You're right, i have mistake
Hello,
What about the map.fitBounds() method? The state is not used in this case. It would be great to have an initialCenter that initiate the map with that center and don't override the next values.
Thanks
@paulduthoit I agree with you. to bypass this problem I put an initial fitBounds value. But it still not clean, we should be able to the inital center on the map.
any updates on this? it would be very beneficial to let the user set the default center