I added a number of markers to the map:

const map = new ol.Map({
    target: 'map',
    layers: [
        new Layer.Tile({source: new OSM()}),
    view: new ol.View({
        center: [0, 0],
        zoom: 2,

// ...

let setMarkers = function (markers) {
    let features = markers.map((marker) => createFeature(...marker));

    // how to fit?

setMarkers(window.markers || []);

After adding the features I would like the map to zoom as much as possible so that all markers are visible at once. How to do that? Previous versions had a zoomToExtent method which is now missing, obviously.

I tried the map.getView().fit(...) method with various inputs, but I didn't manage. The view stays fixed to such location:

View fixed location

I tried, but it does not do anything:

    {constrainResolution: false, padding: [10, 10, 10, 10]}


The map.getView().calculateExtent() returns the same extent initially and after adding the features. So I guess my question is how to calculate the extent properly.


This was as easy as:

const markerSource = new source.Vector();


| improve this answer | |

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.