[OpenLayers-Users] How to remove vector layer properly or justpeieces of a vector layer?

Arnd Wippermann arnd.wippermann at web.de
Wed Oct 31 13:01:42 PDT 2012


you doesn't take the name, but the object
 
map.removeLayer(lineLayer); 
map.destroy(lineLayer); 
 
lineLayer have to be a global variable, what's true in your case.

Arnd
 
 
 
  _____  

Von: openlayers-users-bounces at lists.osgeo.org
[mailto:openlayers-users-bounces at lists.osgeo.org] Im Auftrag von miguel
Gesendet: Mittwoch, 31. Oktober 2012 12:19
An: openlayers-users at lists.osgeo.org
Betreff: [OpenLayers-Users] How to remove vector layer properly or
justpeieces of a vector layer?



 

Hi guys I've been racking my head on how to remove just the vector layer
from my map. Below is my code I  have a function call killLayer(), but it
does not work at all I have try different variations. So I just need to
understand if I add a layer in my init function, how can I get a reference
to that layer to remove later on from another function. In this case my
vector layer. I would like to remove it and just keep the base layer. Any
help would be immensely appreciated.

Miguel

 

My Code:

 

                   var map;

                   var lineLayer = new OpenLayers.Layer.Vector("LineLayer");

                   var mapBounds = new OpenLayers.Bounds( 0.0, -906.0,
800.0, 0.0);

                    var mapMinZoom = 0;

                    var mapMaxZoom = 7;

 

                   // avoid pink tiles

                   OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

                   OpenLayers.Util.onImageLoadErrorColor = "transparent";

 

                   function init(){

                       var navigation_control = new
OpenLayers.Control.Navigation({});

                       var controls_array = [

                                    navigation_control,

                                    new OpenLayers.Control.PanZoomBar({}),

                                    new
OpenLayers.Control.LayerSwitcher({}),

                                    new OpenLayers.Control.Permalink(),

                                    new OpenLayers.Control.MousePosition({})

                                    ];

                

                

                var options = {

                     controls: [],

                       maxExtent: new OpenLayers.Bounds(  0.0, -906.0,
800.0, 0.0 ),

                       maxResolution: 4.000000,

                       numZoomLevels: 8

                          };

    //{controls: controls_array}

                   map = new OpenLayers.Map('map', options ); ;

                     

                //tile map service TMS stands for tile map service.

                   var layer = new OpenLayers.Layer.TMS( "TMS Layer","",

                       {  url: '', serviceVersion: '.', layername: '.',
alpha: true,

                           type: 'png', getURL: overlay_getTileURL 

                           });

                   map.addLayer(layer);

                     map.zoomToExtent( mapBounds );  

                     

                     

                     

                     //create the new layer vector layer which includes all
the drawings.

                     /*here im addding to lines separate line segments.*/

                     

                     map.addLayer(lineLayer);

                     

                     var points = new Array(

                     new OpenLayers.Geometry.Point(56.0000, -244) ,  //x and
y cordinate of first point

                     new OpenLayers.Geometry.Point(368,-248)       //x and y
cordinate of the second point

                                           

                     );

                     

                     var second_segment = new Array(

                     new OpenLayers.Geometry.Point(480,-230),  //x and y
cordinate of first point

                     new OpenLayers.Geometry.Point(668,-250)  //x and y
cordinate of first point

                     );

                     

                     

                     

                     var line  = new OpenLayers.Geometry.LineString(points);

                     var line2  = new
OpenLayers.Geometry.LineString(second_segment);

                     

                     var style={

                     strokeColor: '#0000ff',  

                      strokeOpacity: 0.5, 

                      strokeWidth: 6};

                     

                     var lineFeature = new OpenLayers.Feature.Vector(line,
null, style);

                     var lineFeatureN = new OpenLayers.Feature.Vector(line2,
null, style);

                     lineLayer.addFeatures([lineFeature]);

                     lineLayer.addFeatures([lineFeatureN]);

                     

                

                     

                                     

                     

                 map.addControl(new OpenLayers.Control.PanZoomBar());

                 map.addControl(new OpenLayers.Control.MousePosition());

                 map.addControl(new OpenLayers.Control.MouseDefaults());

                 map.addControl(new OpenLayers.Control.KeyboardDefaults());

 

             }

 

             function killLayer() {

                 //map.removeLayer("LineLayer");

                 //lineLayer.destroy();

                //map.destroy("LineLayer");

                lineLayer.removeFeatures(lineFeature);

            }

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20121031/cd96a2e4/attachment-0001.html>


More information about the Users mailing list