4

Working with OpenLayers 4.2.0, I have the extent [-45930163.095729224, -3380418.677804029, -5390354.729126186, -3011487.2342575104] which is a box around Santa Catarina state, Brazil. enter image description here

But instead of having the map center in this box, I'm actually being centered in Australia, which would be an outer extend, if that is the right word for it, such as the map center as:

enter image description here

My code is (ignore the extent visibility and the condition for "Infinity"):

        extent = [-45930163.095729224, -3380418.677804029, -5390354.729126186, -3011487.2342575104];
        if (extent[0] != "Infinity") {
            map.getView().fit(extent, {size: map.getSize(), padding: [15, 0, 0, 0]});
        }

I've already tried to use a size different than map.getSize(), but all I get is the map zoomed in Australia.

Since I can't comment, @bartvde: It didn't helped too much. Kind of zoomed in, but the center is still in Australia.

1
  • Check your extent coordinates @Fabiano. Those given in your question are not within the validity extent of the EPSG:3857 projection, and are definitely not the corners of the extent shown in your image.
    – ahocevar
    Commented Jul 7, 2017 at 17:48

1 Answer 1

2

Use the constrainResolution: false in the options to fit (second argument).

1
  • thank you very much @bartvde. i got here searching for a means to auto zoom so the extent of my layers would fit by default the entire map view. wish my comment will redirect here people searching the same.
    – alex
    Commented Oct 14, 2023 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.