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

miguel juanabreu at hotmail.com
Wed Oct 31 04:32:32 PDT 2012


Toby my man! You are a genius. I can't tell you how long I've been looking
for this :-D. I looked over and over on the docs and I just could not put it
together. This worked like a charm!

I can't thank you enough for getting me out of this jam.

Thanks

Miguel

 

From: Tobias Reinicke [mailto:ramotswa at gmail.com] 
Sent: Wednesday, October 31, 2012 7:23 AM
To: miguel
Cc: openlayers-users at lists.osgeo.org
Subject: Re: [OpenLayers-Users] How to remove vector layer properly or just
peieces of a vector layer?

 

Hi Miguel,

 

You'll have to use the map getlayersby function to actually grab the layer.

so 

http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.g
etLayersByName

 

so something like (untested)

 

var myLayer = map.getLayersByName('LineLayer');

myLayer[0].destroy();

 

getLayersByName returns an array, and as long as you only have one layer
called "LineLayer" you can use the [0] (first) entry of that array.

 

Give that a go.

 

Toby

 

 

 

On 31 October 2012 11:19, miguel <juanabreu at hotmail.com> wrote:

 

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

            }

 


_______________________________________________
Users mailing list
Users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/openlayers-users

 

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


More information about the Users mailing list