[GIS] How to change events in the Oncomplete Drag function in Openlayers

javascriptmapsopenlayers-2polygonvector

below is code that works for Openlayers drag function:

drag = new OpenLayers.Control.DragFeature(polygonLayer, {
    autoActivate: true,
    onComplete: function() {alert('Are you sure this is the right postion?')}
});

Now i,m trying to change my onComplete function:

drag = new OpenLayers.Control.DragFeature(polygonLayer, {
    autoActivate: true,
    onComplete: function (event){
        var bounds = event.features[0].geometry.getBounds();
        var answer = "bottom: " + bounds.bottom  + "\n";
        answer += "left: " + bounds.left  + "\n";
        answer += "right: " + bounds.right  + "\n";
        answer += "top: " + bounds.top  + "\n";
        alert(answer);
    }
});

I must be doing something wrong as this does not work?

Best Answer

You directly access the feature. So, the following code should work :

  drag = new OpenLayers.Control.DragFeature(polygonLayer, {
     autoActivate: true,
     onComplete: displayBounds
  });

function displayBounds(feature){
        var bounds = feature.geometry.getBounds();
        var answer = "bottom: " + bounds.bottom  + "\n";
        answer += "left: " + bounds.left  + "\n";
        answer += "right: " + bounds.right  + "\n";
        answer += "top: " + bounds.top  + "\n";
        alert(answer);
}
Related Question