How to run a function when finished drawing objects?

I am using openlayers 2 in my project.

I draw 3 types of functions on the map after completing the drawing. I need to call a function called featureDrawed.

This is my code:

drawControl = {
    point: new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector("Point Layer"), OpenLayers.Handler.Point } }),
    line: new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector("Line Layer"), OpenLayers.Handler.Path),
    polygon: new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector("Polygon Layer"), OpenLayers.Handler.Polygon)
}

for (var key in drawControl) {
    control = drawControl[key];
    controls.push(control);
}

Here is the html where the user selects the function to draw:

    <div data-role="popup" id="popupShapes" data-theme="none" style="z-index:999">
        <div data-role="collapsibleset" data-theme="b" data-content-theme="a" style="margin:0; width:250px;">
            <div data-role="listview" data-inset="false">
                <ul data-role="listview">
                    <li>
                        <input type="radio" name="type" value="point" id="pointToggle" />
                        <label for="pointToggle">draw point</label>
                    </li>
                    <li>
                        <input type="radio" name="type" value="line" id="lineToggle" />
                        <label for="lineToggle">draw line</label>
                    </li>
                    <li>
                        <input type="radio" name="type" value="polygon" id="polygonToggle" />
                        <label for="polygonToggle">draw polygon</label>
                    </li>
                </ul>
            </div>
        </div>
    </div>

And here is the event that fires after the user selects the shape to be drawn.

$("#popupShapes ul li input").click(function () {
    var val = $(this).val();

    for (key in drawControl) {
        var control = drawControl[key];
        if (val == key) {
            control.activate();
        } else {
            control.deactivate();
        }
    }
});

The code is working fine. But the problem is that I cannot create an event that is fired after the function is drawn on the map.

+6
source share
2 answers

You can use addEventListener and dispatchEvent. For example:

function DrawSomething(){
/*drawing*/
let event=new Event('featureDrawed');
this.dispatchEvent(event);
}

Somewhere in the code

DrawSomething.addEventListener('featureDrawed', function(){
/*action you want to do when all you want is drawn*/
})

: addEventListener

0

, :

var onDrawEnd = function(e) {
    console.log('feature added', e.feature);
    //your logic
};

:

for (var key in drawControl) {
    control = drawControl[key];
    control.events.register('featureadded', null, onDrawEnd);
    controls.push(control);
}
0

All Articles