首页 > 解决方案 > Openlayers on view fit 动画结束

问题描述

我正在使用openlayers 6.5。

我有:

/* 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();

上面的代码获取县的几何图形并缩放地图,直到县填满地图。如您所见,那里有一个动画设置:duration:1000.

我需要的是在拟合结束后(动画结束后)调用一个函数。

我可以使用setTimeout 1000,但我认为这不是正确的方法。

我需要类似promise...

标签: javascriptopenlayers

解决方案


fit 有一个callback选项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
    }
);

推荐阅读