From 118479aebcf74174fa180561d9d457a9abda1c87 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 27 Feb 2012 09:36:55 +0100 Subject: [PATCH 1/3] Allow map and layer coordinates to be provided as latitude and longitude --- lib/OpenLayers/BaseTypes/Bounds.js | 29 +++++++++++++++++++---- lib/OpenLayers/BaseTypes/LonLat.js | 12 ++++++++++ lib/OpenLayers/Layer.js | 5 ++-- lib/OpenLayers/Map.js | 11 +++++---- tests/BaseTypes/Bounds.html | 12 +++++++++- tests/BaseTypes/LonLat.html | 9 +++++++- tests/Layer.html | 37 ++++++++++++++++++++++++++---- tests/Map.html | 35 +++++++++++++++++++++++++++- 8 files changed, 131 insertions(+), 19 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index a4fee70554..5295f8b43f 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -5,6 +5,7 @@ /** * @requires OpenLayers/BaseTypes/Class.js + * @requires OpenLayers/Projection.js */ /** @@ -72,10 +73,30 @@ OpenLayers.Bounds = OpenLayers.Class({ */ initialize: function(left, bottom, right, top) { if (OpenLayers.Util.isArray(left)) { - top = left[3]; - right = left[2]; - bottom = left[1]; - left = left[0]; + if (left[0] && typeof left[0] === "object" && left[0].lon) { + var transform = arguments[arguments.length-1]; + top = left[1].lat; + right = left[1].lon; + bottom = left[0].lat; + left = left[0].lon; + if (transform instanceof OpenLayers.Projection) { + var bl = OpenLayers.Projection.transform( + {x: left, y: bottom}, "EPSG:4326", transform + ); + var tr = OpenLayers.Projection.transform( + {x: right, y: top}, "EPSG:4326", transform + ); + top = tr.y; + right = tr.x; + bottom = bl.y; + left = bl.x; + } + } else { + top = left[3]; + right = left[2]; + bottom = left[1]; + left = left[0]; + } } if (left != null) { this.left = OpenLayers.Util.toFloat(left); diff --git a/lib/OpenLayers/BaseTypes/LonLat.js b/lib/OpenLayers/BaseTypes/LonLat.js index ed274225c4..957c0bf3e0 100644 --- a/lib/OpenLayers/BaseTypes/LonLat.js +++ b/lib/OpenLayers/BaseTypes/LonLat.js @@ -5,6 +5,7 @@ /** * @requires OpenLayers/BaseTypes/Class.js + * @requires OpenLayers/Projection.js */ /** @@ -45,6 +46,17 @@ OpenLayers.LonLat = OpenLayers.Class({ if (OpenLayers.Util.isArray(lon)) { lat = lon[1]; lon = lon[0]; + } else if (lon && typeof lon === "object" && lon.lon) { + var transform = arguments[arguments.length-1]; + lat = lon.lat; + lon = lon.lon; + if (transform instanceof OpenLayers.Projection) { + var xy = OpenLayers.Projection.transform( + {x: lon, y: lat}, "EPSG:4326", transform + ); + lat = xy.y; + lon = xy.x; + } } this.lon = OpenLayers.Util.toFloat(lon); this.lat = OpenLayers.Util.toFloat(lat); diff --git a/lib/OpenLayers/Layer.js b/lib/OpenLayers/Layer.js index cbaf8e5c16..432844f332 100644 --- a/lib/OpenLayers/Layer.js +++ b/lib/OpenLayers/Layer.js @@ -482,11 +482,12 @@ OpenLayers.Layer = OpenLayers.Class({ OpenLayers.Projection.defaults[newOptions.projection.getCode()]); } // allow array for extents + var srs = this.projection || newOptions.projection; if (newOptions.maxExtent && !(newOptions.maxExtent instanceof OpenLayers.Bounds)) { - newOptions.maxExtent = new OpenLayers.Bounds(newOptions.maxExtent); + newOptions.maxExtent = new OpenLayers.Bounds(newOptions.maxExtent, srs); } if (newOptions.minExtent && !(newOptions.minExtent instanceof OpenLayers.Bounds)) { - newOptions.minExtent = new OpenLayers.Bounds(newOptions.minExtent); + newOptions.minExtent = new OpenLayers.Bounds(newOptions.minExtent, srs); } } diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 02507ad00f..51cdbb723e 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -519,17 +519,18 @@ OpenLayers.Map = OpenLayers.Class({ OpenLayers.Util.applyDefaults(this, OpenLayers.Projection.defaults[projCode]); // allow extents and center to be arrays + var srs = new OpenLayers.Projection(projCode); if (this.maxExtent && !(this.maxExtent instanceof OpenLayers.Bounds)) { - this.maxExtent = new OpenLayers.Bounds(this.maxExtent); + this.maxExtent = new OpenLayers.Bounds(this.maxExtent, srs); } if (this.minExtent && !(this.minExtent instanceof OpenLayers.Bounds)) { this.minExtent = new OpenLayers.Bounds(this.minExtent); } if (this.restrictedExtent && !(this.restrictedExtent instanceof OpenLayers.Bounds)) { - this.restrictedExtent = new OpenLayers.Bounds(this.restrictedExtent); + this.restrictedExtent = new OpenLayers.Bounds(this.restrictedExtent, srs); } if (this.center && !(this.center instanceof OpenLayers.LonLat)) { - this.center = new OpenLayers.LonLat(this.center); + this.center = new OpenLayers.LonLat(this.center, srs); } // initialize layers array @@ -1798,7 +1799,7 @@ OpenLayers.Map = OpenLayers.Class({ */ moveTo: function(lonlat, zoom, options) { if (!(lonlat instanceof OpenLayers.LonLat)) { - lonlat = new OpenLayers.LonLat(lonlat); + lonlat = new OpenLayers.LonLat(lonlat, this.getProjectionObject()); } if (!options) { options = {}; @@ -2319,7 +2320,7 @@ OpenLayers.Map = OpenLayers.Class({ */ zoomToExtent: function(bounds, closest) { if (!(bounds instanceof OpenLayers.Bounds)) { - bounds = new OpenLayers.Bounds(bounds); + bounds = new OpenLayers.Bounds(bounds, this.getProjectionObject()); } var center = bounds.getCenterLonLat(); if (this.baseLayer.wrapDateLine) { diff --git a/tests/BaseTypes/Bounds.html b/tests/BaseTypes/Bounds.html index a1497cf6da..52a216d8f4 100644 --- a/tests/BaseTypes/Bounds.html +++ b/tests/BaseTypes/Bounds.html @@ -4,7 +4,7 @@