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

1

5

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);
Ciri answered 18/7, 2016 at 13:57 Comment(2)
Can you show us your code that isn't working and what you tried exactly ?Reamy
updated an original descriptionCiri
A
11

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

Adiaphorous answered 18/7, 2016 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.