Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
Michael Avatar asked Feb 13 '18 13:02

Michael


1 Answers

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);
}
like image 64
fradal83 Avatar answered Oct 17 '22 15:10

fradal83