Thursday, 5 March 2015

javascript - Openlayers get coordinates of a circle vector after draw


I have made a openlayers map. I have made two features on the map.


Navigate

DRAW POLYGON


I have made 40 sides to my polygon which turns out to be a circle.


After I have drawn my circle I want to show the coordinates of the bounding box of the circle.


So after I draw a circle i want to be able show the top, left, bottom and right coordinates for that circle in a ALERT BOX?


         
















  • value="none" id="noneToggle"
    onclick="polygonControl.deactivate()"
    checked="checked" />




  • value="polygon" id="polygonToggle"
    onclick="polygonControl.activate()" />








Answer




register the featuresadded event on the layer.


var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");

polygonLayer.events.on({
featuresadded: onFeaturesAdded
});

And do this on the function:


function onFeaturesAdded(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);
}

or to get all vertices:


function onFeaturesAdded(event) {
var vertices = event.features[0].geometry.getVertices();

var strVertices = "";
for (var x in vertices) {
strVertices += "(" + vertices[x].x + "," + vertices[x].y + ")";
}

alert(strVertices);
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...