Take the 2-minute tour ×
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(
                OpenLayers.Geometry.fromWKT(
                    "POLYGON ((-18 -3, -18 14, 15 14, 15 -3, -18 -3))"
                )
            );

vectors.addFeatures([feature]);

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

Thanks,

Alfredo

share|improve this question

2 Answers 2

you can use it adding to any trigger:

var dataExtent = vectors.getDataExtent();
map.zoomToExtent(dataExtent);

briefly :

map.zoomToExtent(vectors.getDataExtent());

.

getDataExtent

getDataExtent: function ()

Calculates the max extent which includes all of the features.

.

zoomToExtent

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); } map.events.register("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);
}

vectors.events.register("featureselected", vectors, selected);

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

map.addControl(control);

control.activate();
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

 
discard

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.