From 354817649d00045586c3c11ef46ddc04d4fd6120 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Tue, 11 Dec 2007 07:27:45 +0000 Subject: [PATCH] MousePosition control throws error when no layers are on map. Fix it so that they don't. Thanks for the review, fredj. Includes tests. (Closes #1029) git-svn-id: http://svn.openlayers.org/trunk/openlayers@5371 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/MousePosition.js | 4 ++++ tests/Control/test_MousePosition.html | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/OpenLayers/Control/MousePosition.js b/lib/OpenLayers/Control/MousePosition.js index 4e44e7afc7..861281892b 100644 --- a/lib/OpenLayers/Control/MousePosition.js +++ b/lib/OpenLayers/Control/MousePosition.js @@ -108,6 +108,10 @@ OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { } lonLat = this.map.getLonLatFromPixel(evt.xy); + if (!lonLat) { + // map has not yet been properly initialized + return; + } this.lastXy = evt.xy; } diff --git a/tests/Control/test_MousePosition.html b/tests/Control/test_MousePosition.html index 20b470914e..62b7d721c2 100644 --- a/tests/Control/test_MousePosition.html +++ b/tests/Control/test_MousePosition.html @@ -37,6 +37,23 @@ t.eq(parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Viewport div contains control div"); } + function test_02_Control_MousePosition_redraw_noLayer_displayProjection(t) { + t.plan(3); + control = new OpenLayers.Control.MousePosition(); + map = new OpenLayers.Map('map'); + map.addControl(control); + t.eq(control.div.innerHTML, "0.00000, 0.00000", "innerHTML set correctly"); + control.redraw({'xy': new OpenLayers.Pixel(10,10)}); + control.redraw({'xy': new OpenLayers.Pixel(12,12)}); + t.eq(control.div.innerHTML, "0.00000, 0.00000", "innerHTML set correctly"); + l = new OpenLayers.Layer('name', {'isBaseLayer': true}); + map.addLayer(l); + map.zoomToMaxExtent(); + control.redraw({'xy': new OpenLayers.Pixel(10,10)}); + control.redraw({'xy': new OpenLayers.Pixel(12,12)}); + t.eq(control.div.innerHTML, "-175.78125, 85.78125", "innerHTML set correctly when triggered."); + } +