Michael
Michael

Reputation: 13614

How to fire function when feature drawing complete?

I use openlayers 2 in my project.

I draw 3 types of features on the map after drawing is complete I need to trigger function named featureDrawed.

Her 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 user select the feature 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 event that fired after user select 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 works fine. But the problem is that I can't create event that fired after feature is draw on the map.

Upvotes: 6

Views: 1353

Answers (2)

fradal83
fradal83

Reputation: 2022

Declare the function to be called on draw end:

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

Register a listener for each control:

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

Upvotes: 1

Nikita Vozisov
Nikita Vozisov

Reputation: 66

You can use addEventListener and dispatchEvent. For example:

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

Somewhere in code

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

Note: you need to addEventListener before picture is drawn

Upvotes: 0

Related Questions