The problem was this missing line in my index.html:
Copy<link href="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css" rel="stylesheet">
The documentation tells you nothing about it, had to find it in the source code of this example: https://github.com/visgl/react-map-gl/tree/7.0-release/examples/controls
Answer from Tim MG on Stack OverflowThe problem was this missing line in my index.html:
Copy<link href="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css" rel="stylesheet">
The documentation tells you nothing about it, had to find it in the source code of this example: https://github.com/visgl/react-map-gl/tree/7.0-release/examples/controls
This is not the right solution for me. The solution is very simple
- Step 1: Go to your index.js file
- Step 2: Then add import 'mapbox-gl/dist/mapbox-gl.css';
And just restart the server. The error will surely be resolved.
Use max and min Zoom with NavigationControl
Need Help With react-map-gl mix and max zoom with the <NavigationControl />
reactjs - Implement custom zooming in React-Mapbox-GL - Stack Overflow
How to get current zoom level of the map?
Hello There,
I am working with ReactMapGL and NavigationControl. I am able to set max & min zoom using the ReactMapGL component
< ReactMapGL /> but I am having trouble setting the max & min zoom NavigationControl .
I was looking at the Navigation Control documentation https://visgl.github.io/react-map-gl/docs/api-reference/navigation-control, it does not have any properties for minZoom/maxZoom. Only the ReactMapGL has those properties.
Is there any way I can get that functionally working for?
Thanks
The Map you are using is a ReactMapboxGl object which is a wrapper of mapbox-gl-js. You want to call getZoom() on the mapboxgl.Map object.
To use the original Mapbox API use
onStyleLoadproperty, the callback function will receive the map object as a first arguments, then you can add your own logic alongside react-mapbox-gl. [source]
So you can get it from:
<ReactMapboxGl onStyleLoad={ el => this.map = el } />
Then you could use it here:
componentDidMount() {
const currentZoom = this.map.getZoom();
}
Try like this:
const [zoom, setZoom] = useState(15);
map.current.on('zoom', () => {
setZoom(map.current.getZoom());
});
At the risk of pointing out the obvious, you should be setting minZoom, not maxZoom:
fitBoundsOptions: {linear: true, minZoom: map.getZoom()},
Also, if you're not actually using the geolocation tracking features, or need the control itself, it may be simpler to just use the browser's geolocation API directly:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(e =>
map.jumpTo({ center: [e.coords.longitude, e.coords.latitude]}))
}
I found a slightly different work-around for this. It uses ReactMapGL instead of MapboxGL. The thing that seems to have made the difference is to pass a zoom parameter to a temporary geocoderDefaultOverrides object and then unpacking the viewport and then that object into the return object in handleGeocoderViewportChange. Here are the relevant parts:
handleGeocoderViewportChange = viewport => {
const geocoderDefaultOverrides = { transitionDuration: 0, zoom: 16 };
return this.handleViewportChange({
...viewport,
...geocoderDefaultOverrides
});
};
Then in render:
<Geocoder
mapRef={this.map_ref}
mapboxApiAccessToken={MBAccessToken}
onViewportChange={this.handleGeocoderViewportChange}
viewport={this.state.viewport}
/>
Including the css file fixed this issue for me
import 'mapbox-gl/dist/mapbox-gl.css'
Ok after a few days of messing around I finally figured it out. The issue had something to do with my use of a custom icon for the Marker. Copying over the example's pin made it work correctly.