diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 7451b2f69e..8c7bc91d2a 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -241,10 +241,10 @@ OpenLayers.Map.prototype = { this.moveLayerContainer(latlon); } this.center = latlon.copyOf(); - var zoomChanged = false; - if (zoom != null) { - if (this.zoom && zoom != this.zoom) - zoomChanged = true; + var zoomChanged = null; + if (zoom != null && zoom != this.zoom + && zoom >= 0 && zoom <= this.getZoomLevels()) { + zoomChanged = (this.zoom == null ? 0 : this.zoom); this.zoom = zoom; } @@ -254,17 +254,18 @@ OpenLayers.Map.prototype = { }, moveToNewExtent: function (zoomChanged) { - if (zoomChanged) { // reset the layerContainerDiv's location + if (zoomChanged != null) { // reset the layerContainerDiv's location this.layerContainerDiv.style.left = "0px"; this.layerContainerDiv.style.top = "0px"; } var bounds = this.getExtent(); for (var i = 0; i < this.layers.length; i++) { - this.layers[i].moveTo(bounds, zoomChanged); + this.layers[i].moveTo(bounds, (zoomChanged != null)); } this.events.triggerEvent("move"); - if (zoomChanged) - this.events.triggerEvent("zoomend"); + if (zoomChanged != null) + this.events.triggerEvent("zoomend", + {oldZoom: zoomChanged, newZoom: this.zoom}); }, /** @@ -286,8 +287,7 @@ OpenLayers.Map.prototype = { if (zoom >= 0 && zoom <= this.getZoomLevels()) { var oldZoom = this.zoom; this.zoom = zoom; - this.moveToNewExtent(true); - this.events.triggerEvent("zoomend", {oldZoom: oldZoom, newZoom: this.zoom}); + this.moveToNewExtent(oldZoom); } }, @@ -303,7 +303,7 @@ OpenLayers.Map.prototype = { zoomExtent: function() { var fullExtent = this.getFullExtent(); - + var oldZoom = this.zoom; this.zoom = this.getZoomForExtent( fullExtent ); this.setCenter( new OpenLayers.LatLon( @@ -311,8 +311,6 @@ OpenLayers.Map.prototype = { (fullExtent.minlon+fullExtent.maxlon)/2 ) ); - this.moveToNewExtent(true); - }, /** @@ -337,8 +335,7 @@ OpenLayers.Map.prototype = { */ defaultDblClick: function (evt) { var newCenter = this.getLatLonFromPixel( evt.xy ); - this.zoomIn(); - this.setCenter(newCenter); + this.setCenter(newCenter, this.zoom + 1); }, /** diff --git a/tests/test_Map.html b/tests/test_Map.html index 37d1a1468a..170fa3e7dd 100644 --- a/tests/test_Map.html +++ b/tests/test_Map.html @@ -54,17 +54,27 @@ t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hash" ); } function test_05_Map_center(t) { - t.plan(5); + t.plan(4); map = new OpenLayers.Map($('map')); map.setCenter(new OpenLayers.LatLon(1,2), 0); map.zoomIn(); - t.ok( map.getCenter() instanceof OpenLayers.LatLon, "map.getCenter returns a LatLon"); t.eq( map.getZoom(), 1, "map.zoom is correct after calling setCenter,zoom in"); t.eq( map.getCenter().lat, 1, "map center lat is correct after calling setCenter,zoom in"); t.eq( map.getCenter().lon, 2, "map center lon is correct after calling setCenter, zoom in"); map.zoomOut(); t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out"); } + function test_06_Map_zoomend_event (t) { + t.plan(3); + map = new OpenLayers.Map('map'); + map.events.register("zoomend", {count: 0}, function() { + this.count++; + t.ok(true, "zoomend event was triggered " + this.count + " times"); + }); + map.setCenter(new OpenLayers.LatLon(1,2), 0); + map.zoomIn(); + map.zoomOut(); + } function test_99_Map_destroy (t) { t.plan( 2 ); map = new OpenLayers.Map($('map'));