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.

  • 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
    Jul 7 '17 at 17:48

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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