简体   繁体   English

功能图完成后如何触发功能?

[英]How to fire function when feature drawing complete?

I use openlayers 2 in my project. 我在我的项目中使用openlayers 2。

I draw 3 types of features on the map after drawing is complete I need to trigger function named featureDrawed. 绘图完成后,我在地图上绘制了3种类型的特征我需要触发名为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: 这是用户选择要绘制的特征的html:

    <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. 但问题是我无法创建在地图上绘制要素后触发的事件。

You can use addEventListener and dispatchEvent. 您可以使用addEventListener和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 注意:在绘制图片之前需要addEventListener

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM