AlexeiBerkov
AlexeiBerkov

Reputation: 437

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

I know this question was discussed at many other topics. But I still not able to find a solution for my issue. None of receipts are works for me.

An issue is very simple. I have a features at my Openlayers3 (v3.17.1) map. I should be able to zoom to any of feature I want.

Before zoom:

enter image description here

After zoom:

enter image description here

As I mentioned I use version v3.17.1.

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

Option 1:

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

Option 2:

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

Upvotes: 5

Views: 9773

Answers (1)

Hicham Zouarhi
Hicham Zouarhi

Reputation: 1060

there are many ways to do this, for example if you want to zoom to only one feature and you want to define the level of zoom you can use this :

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
}));

there is another way in which you can zoom to more that just one feature :

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

assuming that in extent you have the extent of your feature or features

Upvotes: 11

Related Questions