如何缩放到 Openlayers3 (v3.17.1) 地图的任何特征

How to zoom to any feature of Openlayers3 (v3.17.1) map

我知道这个问题在许多其他主题中都有讨论。但我仍然无法找到解决我的问题的方法。 None 的收据是我的作品。

一个问题很简单。我的 Openlayers3 (v3.17.1) 地图上有一个功能。我应该能够缩放到我想要的任何功能。

缩放前:

缩放后:

正如我提到的,我使用的是 v3.17.1 版本。

var features = myLayer.getSource().getFeatures();
var myFirstFeature = features[0];

选项 1:

map.zoomToExtent(feature.geometry.getBounds());

选项 2:

    var bounds = f.geometry.getBounds().clone();
    this._map.zoomToExtent(bounds,false);

有很多方法可以做到这一点,例如,如果您只想缩放到一个要素并且想要定义缩放级别,您可以使用此方法:

var ext=feature.getGeometry().getExtent();
var center=ol.extent.getCenter(ext);
map.setView( new ol.View({
    projection: 'EPSG:4326',//or any projection you are using
    center: [center[0] , center[1]],//zoom to the center of your feature
    zoom: 12 //here you define the levelof zoom
}));

还有另一种方法可以缩放到更多,而不仅仅是一个特征:

map.getView().fit(extent, map.getSize());

假设在范围内您拥有一个或多个特征的范围