0

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?

  • How does it not work? What are the error messages - if any? – Chau Feb 9 '12 at 14:39
  • Don't get an error, I am able to drag my polygon, but when i drop it on a spot on the map , it does nothing as where my first alert works when i drop the polygon on the map. – Blake Loizides Feb 9 '12 at 15:20
  • @Chau see post above. – Blake Loizides Feb 9 '12 at 15:52
4

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);
}
  • That was an excellent answer:) Still new to openlayers. – Blake Loizides Feb 9 '12 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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