1

I want to draw multiple buffer selection on click so that each selection is saved in a different vector layer and buffer features in one vector layer. The following code works well for the first buffer and selection layer. But on second time two empty selection are created and 3rd time 3 selection empty layers and so on, what I could figure out is that featureadded event for the buffer layer is called no of times of the total features in bufferLayer every time.

map.addLayers([bufferLayer, pointLayer]);
pointCtrl = new OpenLayers.Control.DrawFeature(
        pointLayer, OpenLayers.Handler.Point, {
        eventListeners : {
            //beforefeatureadded    : function (e) {e.attributes = { id: featureId++, name : "Buffer Point "+featureId, radius: radius, sides:sides }},
            featureadded        : function (e) {
                var center_point = new OpenLayers.Geometry.Point(e.feature.geometry.x, e.feature.geometry.y);
                var circle = new OpenLayers.Geometry.Polygon.createRegularPolygon(center_point, radius, sides, 0);
                var circleFeature = new OpenLayers.Feature.Vector(circle);
                circleFeature.attributes = { id: featureId, name : "Buffer "+featureId, radius: radius, sides:sides };
                bufferLayer.events.on({
                    featureadded        : function(e){
                        var x;
                        var geometry = e.feature.geometry.clone();
                        var spatialfilter = new OpenLayers.Filter.Spatial({type : OpenLayers.Filter.Spatial.INTERSECTS,value : geometry.transform(mercator, geographic)});
                        bufferFilter =      new OpenLayers.Filter.Logical({type : OpenLayers.Filter.Logical.AND, filters : [spatialfilter, getGeneralFilter()]});
                        var x;
                        var cloneLayer=hfLayer.clone();
                        cloneLayer.filter = bufferFilter;
                        cloneLayer.name="Buffer Selection "+featureId++;
                        console.log(featureId);
                        cloneLayer.refresh({force : true});
                        cloneLayer.events.on(
                                {
                                featuresadded : function (event) {
                                    pointCtrl.deactivate();
                                    pointLayer.styleMap.styles.default.rules[0].symbolizer.label = "Health Facilities\n Within " + radius / 1000 + " Kilometers\n" + cloneLayer.features.length;
                                    map.zoomToExtent(bufferLayer.getDataExtent());
                                }, scope: this
                                });
                        cloneLayer.refresh({force : true});
                        map.addLayers([cloneLayer]);
                    },
                        scope : this
                });
                bufferLayer.addFeatures(circleFeature);
                pointCtrl.deactivate();

            },
            scope : this
        }
    });
map.addControl(pointCtrl);
pointCtrl.activate();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.