mapboxfitbounds

Mapbox fitBounds does not accept coordinates - stays in Kenya


I just started with MapBox and have this code (simply copied the code from this sample https://docs.mapbox.com/mapbox-gl-js/example/fitbounds/ and replaced the coords):

document.getElementById('fit').addEventListener('click', function () {
map.fitBounds([
[43.965830, -9.839989],
[36.150694, 5.506686]
]);

This should give the map of Spain, alas I still end up in Kenya

Where might the error be?

Thanks


Solution

  • Your coordinates are reversed... try this and it will smoothly jump to Spain

        document.getElementById('fit').addEventListener('click', function () {
            map.fitBounds([
                [-9.839989, 43.965830],
                [5.506686, 36.150694]
            ]);
        });
    

    enter image description here