Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.