如何处理openlayers绘制多边形事件

时间:2014-08-11 12:11:27

标签: javascript maps openlayers

我在我的OpenLayers应用程序中有这样的绘制控件。

    var drawControls = {
        polygon: new OpenLayers.Control.DrawFeature(polygonLayer, OpenLayers.Handler.Polygon),
        box: new OpenLayers.Control.DrawFeature(boxLayer, OpenLayers.Handler.RegularPolygon,{
            handlerOptions: {
                sides: 4,
                irregular: true
            }
        })
    };

我正在使用激活控制功能激活此控件。

 var control = drawControls[selected.key];
 control.activate();

这会在地图上绘制多边形。但我无法访问此控件的事件。例如,在绘制结束时,事件应该给我一个多边形或框。

1 个答案:

答案 0 :(得分:2)

以下是使用控件的featureadded事件的示例。

    // Add a polygon layer to which polygons will be drawn.
    var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer", {
        projection: "EPSG:4326"
    });
    map.addLayers([polygonLayer]);

    // Initialize the polygon editor.
    var polygonEditor = new OpenLayers.Control.DrawFeature(polygonLayer,
        OpenLayers.Handler.Polygon);
    // And its event listener when the feature is added.
    polygonEditor.events.register('featureadded', polygonEditor, function(evt) {
        // Here, you should see the geometry of the drawn feature in your console.
        var geom = evt.feature.geometry;
        console.log(geom);
    });

示例fiddle