简体   繁体   中英

Activation/deactivation of OpenLayers.Controls.DrawFeature fails

When I break on the first line in the call back function and do this

this.active --> true
this.deactivate() --> true
this.activate() --> true
this.deactivate() --> false

HOW CAN THIS HAPPEN?

Let me explain where this occurs.

This map has two layers and each layer has an EditingToolbar associated with it.

When the user pressed polygon draw (in the EditingToolbar), its being draw on the "polygon_layer".

When the user presses line draw or point draw the layer is switched and the user now draws on the "vectors" layer.

When I switch layers, I need to activate the correct button in the EditingToolbar, example:

The user draws polygons in the "polygon_layer" and now he want to draw lines. He presses draw lines button (in the EditingToolbar associated with the polygon_layer).

I switch layers and activate draw lines button on the EditingToolbar for that layer.

After a while, the user now wants to draw polygons again, so i deactivate all buttons in this layer, switch layers and activate the draw polygon button in the EditingToolbar for the polygon_layer. and so on.

Now when I do this enough times (3 switches) I notice that the buttons don't get deactivate anymore.

So I tried to debug and got this totally unexpected error described above ( at the very top).

Please tell me what am I doing wrong.

I've appended my code and I'll put ERROR HERE where this occurs. This code is ready to run. You can use the HTML code below, just change the reference to the JavaScript file that I've provided ( I've provided the contents of the JavaScript file)

JS FILE:
var map;
var editing_toolbar_polygon=null;
var editing_toolbar_vector=null;
var drag_control=null;
var vectors;
var polygon_layer=null;
var epsg900913 = new OpenLayers.Projection('EPSG:900913');
var epsg4326 = new OpenLayers.Projection('EPSG:4326');
//var epsg900913 = new OpenLayers.Projection('EPSG:900913');
// var epsg4326 = new OpenLayers.Projection('EPSG:4326');
var line_control;
var polygon_control;
var renderer;
function initialize() {
    line_control, renderer=OpenLayers.Util.getParameters(window.location.href).renderer;
    renderer= (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
    // Create the map object
    map = new OpenLayers.Map('map');
     //Create a Google layer
    var gmap = new OpenLayers.Layer.Google(
        "Google Streets", // the default
        {
            numZoomLevels: 20,
            projection: new OpenLayers.Projection("EPSG:900913")
        }
    );
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
             "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic',
    projection: new OpenLayers.Projection("EPSG:4326")});

    var mystyle=new OpenLayers.StyleMap({
        "default": new OpenLayers.Style({
             fillColor: "#66ccff",
             strokeColor: "#3399ff",
             graphicZIndex: 2,
             strokeWidth: 5,
         }),
        "temporary": new OpenLayers.Style({
             fillColor:"#3399ff",
             strokeColor: "#3399ff",
             strokeWidth:5,  
             pointRadius:10

         })        
    });
    polygon_layer=new OpenLayers.Layer.Vector(
        "Polygon Layer",
        {
            //renderers:renderer,
        }
    );

    vectors= new OpenLayers.Layer.Vector(
        "Vector Layer",
        {
            //renderers:renderer,
        }
    );

    editing_toolbar_polygon=new OpenLayers.Control.EditingToolbar(polygon_layer);
    editing_toolbar_vector=new OpenLayers.Control.EditingToolbar(vectors);
    map.addLayers([gmap,wms,vectors,polygon_layer]);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    //map.addControl(new OpenLayers.Control.MousePosition());
    map.addControl(editing_toolbar_polygon); 
    map.addControl(editing_toolbar_vector); 
    editing_toolbar_vector.deactivate();
    //for the drag control to work you need to activate it
    drag_control=new OpenLayers.Control.DragFeature(vectors);
    map.addControl(drag_control);
    find_control(editing_toolbar_polygon.getControlsByClass(new RegExp(".*DrawFeature")),"Point").events.register("activate",null,function(e){
      //ERROR HERE
        this.deactivate();
        var picked_button=find_same_control(editing_toolbar_vector.controls,e.object);
        change_layer(polygon_layer,vectors);
        change_control(editing_toolbar_polygon,editing_toolbar_vector);
        picked_button.activate();
    });
    find_control(editing_toolbar_polygon.getControlsByClass(new RegExp(".*DrawFeature")),"Path").events.register("activate",null,function(e){
      //ERROR HERE
        this.deactivate();
        var picked_button=find_same_control(editing_toolbar_vector.controls,e.object);
        change_layer(polygon_layer,vectors);
        change_control(editing_toolbar_polygon,editing_toolbar_vector);
        picked_button.activate();
    });
    find_control(editing_toolbar_vector.getControlsByClass(new RegExp(".*DrawFeature")),"Polygon").events.register("activate",null,function(e){
      //ERROR HERE
        this.deactivate();
        var picked_button=find_same_control(editing_toolbar_polygon.controls,e.object);
        change_layer(vectors,polygon_layer);
        change_control(editing_toolbar_vector,editing_toolbar_polygon);
        picked_button.activate();
    });
    polygon_layer.events.register("beforefeatureadded",null,function(e){  
            polygon_layer.removeAllFeatures();
    });
   // line_control=new OpenLayers.Control.DrawFeature(vectors,OpenLayers.Handler.Path);
    //polygon_control=new OpenLayers.Control.DrawFeature(vectors,OpenLayers.Handler.RegularPolygon);
    //map.addControl(line_control);
    //line_control.activate();
    //map.addControl(polygon_control);
    //polygon_control.activate();
    // Zoom to Vancouver, BC
    map.setCenter(new OpenLayers.LonLat(-123.12, 49.28).transform(epsg4326, epsg900913), 13);         

}

function change_layer(current_layer,next_layer){
   current_layer.setVisibility(false);
   next_layer.setVisibility(true); 
}
function change_control(current_control,next_control){
   current_control.deactivate();
   map.addControl(next_control);
}

//use this when you want to find a specific control type:
// DrawFeature cntrol has many types, Line, Polygon, Point.
// So what you do is pass an array of DrawFeature controls and a type(string), lets say "Point",
// then this function will return a DrawFeature thats specifically for drawing points
function find_control(controls,type){
    var control;
    for(var x in controls){
        if(controls[x].displayClass.search(new RegExp(type+"$"))>0){
             return controls[x];
        }
    }
    return -1;
}

I just tried with a simple test. When you desactivate a controller, you just remove the events of the controller. So for OpenLayers.Control.LayerSwitcher, desactivate this controller is useless because nothing change and you can still choose a layer.

I think the best way is to remove the controller for your polygon and to add that for the lines.

When you select the "polygon_layer" :

map.addControl(editing_toolbar_polygon); 
map.removeControl(editing_toolbar_vector); 

When you select the "vectors" :

map.addControl(editing_toolbar_vector); 
map.removeControl(editing_toolbar_polygon); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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