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

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


 

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/6f032572/attachment-0001.html>


More information about the Users mailing list