Search code examples
javascriptopenlayers

Openlayers on view fit animation ends


I'm using openlayers 6.5.

I have:

/* Set the features to fill the map */
var extent = ol.extent.createEmpty();
for(var county_id in map['map_internal_data'])
{
    ol.extent.extend(
        extent,
        map['openlayers_features']['counties'][county_id].getGeometry().getExtent()
    );
}
map['openlayers'].getView().fit(
    extent,
    {
        size: map['openlayers'].getSize(),
        padding: [25, 10, 25, 10],
        duration:1000
    }
);

/* I need to call a function here, but only after above animation ends */
function_after_above_animation_ends();

The above code takes geometry of counties and zoom the map until the counties fill the map. As you see, there is an animation setting there: duration:1000.

What I need is to call a function after fit ends (after animation ends).

I can use setTimeout 1000, but I don't think this is the properly way to do this.

I need something like a promise...


Solution

  • fit has a callback option https://openlayers.org/en/latest/apidoc/module-ol_View-View.html#fit

    map['openlayers'].getView().fit(
        extent,
        {
            size: map['openlayers'].getSize(),
            padding: [25, 10, 25, 10],
            duration:1000,
            callback: myFunction
        }
    );