diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index d79e559c9b..a9952cb36a 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -486,6 +486,9 @@ OpenLayers.Map = OpenLayers.Class({ // now override default options OpenLayers.Util.extend(this, options); + // initialize layers array + this.layers = []; + this.id = OpenLayers.Util.createUniqueID("OpenLayers.Map_"); this.div = OpenLayers.Util.getElement(div); @@ -564,8 +567,6 @@ OpenLayers.Map = OpenLayers.Class({ document.getElementsByTagName('head')[0].appendChild(cssNode); } } - - this.layers = []; if (this.controls == null) { if (OpenLayers.Control != null) { // running full or lite? @@ -590,6 +591,17 @@ OpenLayers.Map = OpenLayers.Class({ // always call map.destroy() OpenLayers.Event.observe(window, 'unload', this.unloadDestroy); + + // add any initial layers + if (options && options.layers) { + this.addLayers(options.layers); + } + + // set center (and optionally zoom) + if (options && options.center) { + // zoom can be undefined here + this.setCenter(options.center, options.zoom); + } }, /** diff --git a/tests/Map.html b/tests/Map.html index 7cfc70d108..7dfb3ce660 100644 --- a/tests/Map.html +++ b/tests/Map.html @@ -1493,6 +1493,101 @@ map.destroy(); } } + + function test_layers_option(t) { + + t.plan(3); + + var map = new OpenLayers.Map({ + div: "map", + layers: [ + new OpenLayers.Layer() + ] + }); + + t.eq(map.layers.length, 1, "single layer from options added"); + + map.destroy(); + + map = new OpenLayers.Map({ + div: "map", + layers: [ + new OpenLayers.Layer(null, {isBaseLayer: true}), + new OpenLayers.Layer(null, {isBaseLayer: false}) + ] + }); + + t.eq(map.layers.length, 2, "multiple layers added from options"); + t.ok(map.baseLayer, "map has a base layer"); + + } + + function test_center_option(t) { + t.plan(6); + + var map, msg; + + + // try setting center without layers, this is not supported + var failed = false; + try { + map = new OpenLayers.Map({ + div: "map", + center: new OpenLayers.LonLat(1, 2) + }); + msg = "center set with no layers"; + } catch (err) { + failed = true; + msg = "center cannot be set without layers"; + } + t.ok(failed, msg); + + if (map) { + map.destroy(); + } + + + // set center without zoom + var center = new OpenLayers.LonLat(1, 2); + map = new OpenLayers.Map({ + div: "map", + layers: [new OpenLayers.Layer(null, {isBaseLayer: true})], + center: center + }); + + t.ok(center.equals(map.getCenter()), "map center set without zoom"); + + map.destroy(); + + // set center and zoom + var zoom = 3; + map = new OpenLayers.Map({ + div: "map", + layers: [new OpenLayers.Layer(null, {isBaseLayer: true})], + center: center, + zoom: zoom + }); + + t.ok(center.equals(map.getCenter()), "map center set with center and zoom"); + t.eq(zoom, map.getZoom(), "map zoom set with center and zoom"); + + map.destroy(); + + // set center and zoom with all overlays + map = new OpenLayers.Map({ + div: "map", + allOverlays: true, + layers: [new OpenLayers.Layer()], + center: center, + zoom: zoom + }); + + t.ok(center.equals(map.getCenter()), "map center set with all overlays"); + t.eq(zoom, map.getZoom(), "map zoom set with all overlays"); + + map.destroy(); + + }