react-mapbox-gl icon indicating copy to clipboard operation
react-mapbox-gl copied to clipboard

Support for initial zoom/center

Open joshwcomeau opened this issue 9 years ago • 11 comments

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.

joshwcomeau avatar Dec 18 '16 16:12 joshwcomeau

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.

alex3165 avatar Dec 18 '16 21:12 alex3165

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.

joshwcomeau avatar Dec 18 '16 21:12 joshwcomeau

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}
>

alex3165 avatar Dec 18 '16 21:12 alex3165

Ohhh. Right, that makes total sense.

Sorry to have disturbed you! Happy to learn that it was my mistake :)

joshwcomeau avatar Dec 18 '16 21:12 joshwcomeau

No problem happy to solve your problem 👍

alex3165 avatar Dec 18 '16 22:12 alex3165

But if you update the state using setState the reference still the same right? So the center will not move

sohibul avatar Mar 21 '17 04:03 sohibul

Nope, you will break the reference equality if you assign a new array :

this.setState({
  zoom: [new_zoom_value]
});

alex3165 avatar Mar 21 '17 09:03 alex3165

You're right, i have mistake

sohibul avatar Mar 21 '17 09:03 sohibul

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 avatar Nov 02 '18 11:11 paulduthoit

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

Kranysys avatar Feb 11 '20 11:02 Kranysys

any updates on this? it would be very beneficial to let the user set the default center

asalpha avatar Jan 08 '21 20:01 asalpha