diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index b6b1e650ec..1a834b6a4f 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -233,6 +233,24 @@ OpenLayers.Map.prototype = { /* */ /********************************************************/ + /** + * @param {String} name + * + * @returns The Layer with the corresponding id from the map's + * layer collection, or null if not found. + * @type OpenLayers.Layer + */ + getLayer: function(id) { + var foundLayer = null; + for (var i = 0; i < this.layers.length; i++) { + var layer = this.layers[i]; + if (layer.id == id) { + foundLayer = layer; + } + } + return foundLayer; + }, + /** * @param {OpenLayers.Layer} layer */ diff --git a/tests/test_Map.html b/tests/test_Map.html index 031ce07918..5311bfadfc 100644 --- a/tests/test_Map.html +++ b/tests/test_Map.html @@ -205,6 +205,24 @@ t.ok( !map.isValidLonLat(new OpenLayers.LonLat(10, 10)), "lonlat outside max extent is not valid" ); } + function test_10_Map_getLayer(t) { + t.plan( 2 ); + map = new OpenLayers.Map($('map')); + layer = new OpenLayers.Layer.WMS('Test Layer', + "http://octo.metacarta.com/cgi-bin/mapserv", + {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, + {maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } ); + + map.addLayer(layer); + var gotLayer = map.getLayer(layer.id); + + t.ok( layer == gotLayer, "getLayer correctly returns layer" ); + + gotLayer = map.getLayer("chicken"); + + t.ok( gotLayer == null, "getLayer correctly returns null when layer not found"); + } + function test_99_Map_destroy (t) { t.plan( 2 ); map = new OpenLayers.Map($('map'));