Allowing layers, center, and zoom to be set in the layer constructor. r=ahocevar (closes #2480)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10044 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
+14
-2
@@ -486,6 +486,9 @@ OpenLayers.Map = OpenLayers.Class({
|
|||||||
// now override default options
|
// now override default options
|
||||||
OpenLayers.Util.extend(this, options);
|
OpenLayers.Util.extend(this, options);
|
||||||
|
|
||||||
|
// initialize layers array
|
||||||
|
this.layers = [];
|
||||||
|
|
||||||
this.id = OpenLayers.Util.createUniqueID("OpenLayers.Map_");
|
this.id = OpenLayers.Util.createUniqueID("OpenLayers.Map_");
|
||||||
|
|
||||||
this.div = OpenLayers.Util.getElement(div);
|
this.div = OpenLayers.Util.getElement(div);
|
||||||
@@ -565,8 +568,6 @@ OpenLayers.Map = OpenLayers.Class({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.layers = [];
|
|
||||||
|
|
||||||
if (this.controls == null) {
|
if (this.controls == null) {
|
||||||
if (OpenLayers.Control != null) { // running full or lite?
|
if (OpenLayers.Control != null) { // running full or lite?
|
||||||
this.controls = [ new OpenLayers.Control.Navigation(),
|
this.controls = [ new OpenLayers.Control.Navigation(),
|
||||||
@@ -590,6 +591,17 @@ OpenLayers.Map = OpenLayers.Class({
|
|||||||
|
|
||||||
// always call map.destroy()
|
// always call map.destroy()
|
||||||
OpenLayers.Event.observe(window, 'unload', this.unloadDestroy);
|
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);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1494,6 +1494,101 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user