Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using openLayer js to draw a polygon and now i want map extents of the drawn polygon one final click (i.e. user finish drawing polygon or double click is done). For drawing the polygon i am using following code:

action = Ext.create('GeoExt.Action', {
        text: "Draw Poly",
        icon: 'Images/poly.png',
        control: new OpenLayers.Control.DrawFeature(
            vector, OpenLayers.Handler.Polygon
        map: map,
        // button options
        toggleGroup: "draw",
        allowDepress: false,
        tooltip: "Draw Polygon",
        // check item options
        group: "draw"

this code is written using openlayer js, extjs and geoextjs. Any please tell how to get map extends for the drawn polygon.

share|improve this question
Do you want the extent, after the user has drawn the polygon? have you looked at the Polygon.getBounds() function? –  Devdatta Tengshe Apr 26 '13 at 5:49
yes i want the polygon extends after user finish drawing. i have to do once user finish drawing in this case Polygon.getBounds() will help? how please guide me or give sample reference. –  Pari Apr 26 '13 at 5:53

1 Answer 1

up vote 2 down vote accepted

You need to listen to the featureadded event of the vector layer, and then get the geometry there.

Have a look at the following JavaScript code:

var map;
function init(){
    map = new OpenLayers.Map('map');

    var wmsLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "", {layers: 'basic'});

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

    map.addControl(new OpenLayers.Control.MousePosition());

   var  polygon= new OpenLayers.Control.DrawFeature(polygonLayer,OpenLayers.Handler.Polygon);


    map.setCenter(new OpenLayers.LonLat(0, 0), 3);

    //activate the control

    //register the event listner'featureadded',polygonLayer, onAdded);

function onAdded(ev){
    var polygon=ev.feature.geometry;
    var bounds=polygon.getBounds();
    //now do whatever you want with the bounds
    //I'm just logging it to the console                

You can also see this working example:

share|improve this answer
Thanks for this, but how i will use using openlayer js , extjs and geoext js. please see my code for that. –  Pari Apr 26 '13 at 6:36
You have a vector layer called 'vector', right? Then just call'featureadded',vector, onAdded); and add the required onAdded function somewhere in your js file. –  Devdatta Tengshe Apr 26 '13 at 6:38
Thanks in Tone it worked. –  Pari Apr 26 '13 at 6:46
Please consider marking this answer as accepted. –  Devdatta Tengshe Apr 26 '13 at 7:07
sorry for that. please see if accepted is checked now –  Pari Apr 26 '13 at 7:13

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.