Hi Miguel,<div><br></div><div>You'll have to use the map getlayersby function to actually grab the layer.</div><div>so </div><div><a href="http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.getLayersByName">http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.getLayersByName</a><br>
</div><div><br></div><div>so something like (untested)</div><div><br></div><div>var myLayer = map.getLayersByName('LineLayer');</div><div>myLayer[0].destroy();</div><div><br></div><div>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.</div>
<div><br></div><div>Give that a go.</div><div><br></div><div>Toby</div><div><br></div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 31 October 2012 11:19, miguel <span dir="ltr"><<a href="mailto:juanabreu@hotmail.com" target="_blank">juanabreu@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas">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.<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas">Miguel<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas">My Code:<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var map;<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var lineLayer = new OpenLayers.Layer.Vector("LineLayer");<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var mapBounds = new OpenLayers.Bounds( 0.0, -906.0, 800.0, 0.0);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> var mapMinZoom = 0;<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var mapMaxZoom = 7;<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> // avoid pink tiles<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> OpenLayers.Util.onImageLoadErrorColor = "transparent";<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> function init(){<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var navigation_control = new OpenLayers.Control.Navigation({});<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var controls_array = [<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> navigation_control,<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Control.PanZoomBar({}),<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Control.LayerSwitcher({}),<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Control.Permalink(),<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Control.MousePosition({})<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> ];<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var options = {<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> controls: [],<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> maxExtent: new OpenLayers.Bounds( 0.0, -906.0, 800.0, 0.0 ),<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> maxResolution: 4.000000,<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> numZoomLevels: 8<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> };<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> //{controls: controls_array}<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map = new OpenLayers.Map('map', options ); ;<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> //tile map service TMS stands for tile map service.<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var layer = new OpenLayers.Layer.TMS( "TMS Layer","",<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> { url: '', serviceVersion: '.', layername: '.', alpha: true,<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> type: 'png', getURL: overlay_getTileURL <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> });<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.addLayer(layer);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.zoomToExtent( mapBounds ); <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> //create the new layer vector layer which includes all the drawings.<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> /*here im addding to lines separate line segments.*/<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.addLayer(lineLayer);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var points = new Array(<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Geometry.Point(56.0000, -244) , //x and y cordinate of first point<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Geometry.Point(368,-248) //x and y cordinate of the second point<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> );<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var second_segment = new Array(<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Geometry.Point(480,-230), //x and y cordinate of first point<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> new OpenLayers.Geometry.Point(668,-250) //x and y cordinate of first point<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> );<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var line = new OpenLayers.Geometry.LineString(points);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> var line2 = new OpenLayers.Geometry.LineString(second_segment);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var style={<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> strokeColor: '#0000ff', <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> strokeOpacity: 0.5, <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> strokeWidth: 6};<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var lineFeature = new OpenLayers.Feature.Vector(line, null, style);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> var lineFeatureN = new OpenLayers.Feature.Vector(line2, null, style);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> lineLayer.addFeatures([lineFeature]);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> lineLayer.addFeatures([lineFeatureN]);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> <u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.addControl(new OpenLayers.Control.PanZoomBar());<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.addControl(new OpenLayers.Control.MousePosition());<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> map.addControl(new OpenLayers.Control.MouseDefaults());<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> map.addControl(new OpenLayers.Control.KeyboardDefaults());<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> }<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> function killLayer() {<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> //map.removeLayer("LineLayer");<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> //lineLayer.destroy();<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> //map.destroy("LineLayer");<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"> lineLayer.removeFeatures(lineFeature);<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:12.0pt;font-family:Consolas"> }<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p></div></div><br>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.osgeo.org">Users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-users" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-users</a><br>
<br></blockquote></div><br></div>