Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

if I have this vector layer:

var vectors = new OpenLayers.Layer.Vector("vector", {isBaseLayer: false});

var feature = new OpenLayers.Feature.Vector(
                    "POLYGON ((-18 -3, -18 14, 15 14, 15 -3, -18 -3))"


How I can zoom to selected feature of the layer by a single click event?



share|improve this question

2 Answers 2

you can use it adding to any trigger:

var dataExtent = vectors.getDataExtent();

briefly :




getDataExtent: function ()

Calculates the max extent which includes all of the features.



zoomToExtent: function(   bounds,
  closest )

Zoom to the passed in bounds, recenter

i hope it helps you...

share|improve this answer
I'm trying like this:function zoomFunction(evt) { var dataExtent = vectors.features[i].geometry.getBounds(); map.zoomToExtent(dataExtent, closest=true); }"click", map, zoomFunction); How can I get the feature order from the vectors? – Alfredo May 4 '12 at 15:26
what is that, i didnt understand? isnt it work? pls give some info... – Aragon May 4 '12 at 15:30

I found the solution:

function selected (evt) {
    map.zoomToExtent(evt.feature.geometry.getBounds(), closest=true);
}"featureselected", vectors, selected);

var control = new OpenLayers.Control.SelectFeature(vectors);


share|improve this answer
you have expressed the question in wrong way. Your question must be "zoom to selected feature". i have gave you code about zooming to the layer. – Aragon May 4 '12 at 17:06
I've change my question. Thanks – Alfredo May 7 '12 at 9:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.