Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/OpenLayers/BaseTypes/Bounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/**
* @requires OpenLayers/BaseTypes/Class.js
* @requires OpenLayers/Projection.js
*/

/**
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions lib/OpenLayers/BaseTypes/LonLat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/**
* @requires OpenLayers/BaseTypes/Class.js
* @requires OpenLayers/Projection.js
*/

/**
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 22 additions & 8 deletions lib/OpenLayers/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,23 @@ OpenLayers.Layer = OpenLayers.Class({

/**
* APIProperty: maxExtent
* {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* The maximum extent for the layer. Defaults to null.
* {<OpenLayers.Bounds>|Array} If provided as an array of four
* values or as <OpenLayers.Bounds>, coordinates are expected to be in
* the layer's <projection>. If provided as array of four values, the
* order of the values is left, bottom, right, top. The bounds can
* also be provided as an array of two values. In this case the first
* value is an object literal with lat and lon (WGS84 latitude and
* longitude) for the bottom left corner, and another one for the top
* right corner.
*
* On a layer with projection set to "EPSG:3857", the following three
* statements are equivalent:
*
* (code)
* maxExtent: [{lat: -80, lon: -170}, {lat: 80, lon: 170}]
* maxExtent: [-18924313.432222, -15538711.094146, 18924313.432222, 15538711.094146]
* maxExtent: new OpenLayers.Bounds(-18924313.432222, -15538711.094146, 18924313.432222, 15538711.094146)
* (end)
*
* The center of these bounds will not stray outside
* of the viewport extent during panning. In addition, if
Expand All @@ -242,8 +256,7 @@ OpenLayers.Layer = OpenLayers.Class({

/**
* APIProperty: minExtent
* {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* {<OpenLayers.Bounds>|Array} See <maxExtent> for supported values.
* The minimum extent for the layer. Defaults to null.
*/
minExtent: null,
Expand Down Expand Up @@ -333,7 +346,7 @@ OpenLayers.Layer = OpenLayers.Class({
*
* Parameters:
* name - {String} The layer name
* options - {Object} Hashtable of extra options to tag onto the layer
* options - {Object} Hashtable of extra options to tag onto the layer.
*/
initialize: function(name, options) {

Expand Down Expand Up @@ -482,11 +495,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);
}
}

Expand Down
80 changes: 53 additions & 27 deletions lib/OpenLayers/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ OpenLayers.Map = OpenLayers.Class({

/**
* APIProperty: maxExtent
* {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* {<OpenLayers.Bounds>|Array} See <zoomToExtent> for supported values.
* The maximum extent for the map. Defaults to the
* whole world in decimal degrees (-180, -90, 180, 90). Specify a
* different extent in the map options if you are not using a geographic
Expand All @@ -320,16 +319,14 @@ OpenLayers.Map = OpenLayers.Class({

/**
* APIProperty: minExtent
* {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* {<OpenLayers.Bounds>|Array} See <zoomToExtent> for supported values.
* The minimum extent for the map. Defaults to null.
*/
minExtent: null,

/**
* APIProperty: restrictedExtent
* {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* {<OpenLayers.Bounds>|Array} See <zoomToExtent> for supported values.
* Limit map navigation to this extent where possible.
* If a non-null restrictedExtent is set, panning will be restricted
* to the given bounds. In addition, zooming to a resolution that
Expand Down Expand Up @@ -443,9 +440,8 @@ OpenLayers.Map = OpenLayers.Class({
* options - {Object} Optional object with properties to tag onto the map.
*
* Valid options (in addition to the listed API properties):
* center - {<OpenLayers.LonLat>|Array} The default initial center of the map.
* If provided as array, the first value is the x coordinate,
* and the 2nd value is the y coordinate.
* center - {<OpenLayers.LonLat>|Array|Object} The initial center of the
* map. See <setCenter> for supported values.
* Only specify if <layers> is provided.
* Note that if an ArgParser/Permalink control is present,
* and the querystring contains coordinates, center will be set
Expand All @@ -455,9 +451,9 @@ OpenLayers.Map = OpenLayers.Class({
* Note that if an ArgParser/Permalink control is present,
* and the querystring contains a zoom level, zoom will be set
* by that, and this option will be ignored.
* extent - {<OpenLayers.Bounds>|Array} The initial extent of the map.
* If provided as an array, the array should consist of
* four values (left, bottom, right, top).
* extent - {<OpenLayers.Bounds>|Array} The initial extent of the map. Only
* specify if <center> and <zoom> are not provided. See <zoomToExtent>
* for supported values.
* Only specify if <center> and <zoom> are not provided.
*
* Examples:
Expand All @@ -483,10 +479,13 @@ OpenLayers.Map = OpenLayers.Class({
* center: [-12356463.476333, 5621521.4854095]
* });
*
* // create a map without a reference to a container - call render later
* // create a map without a reference to a container - call render later.
* // use latitude/longitude objects for bounds and center
* var map = new OpenLayers.Map({
* projection: "EPSG:3857",
* maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000)
* maxExtent: [{lat: -80, lon: -170}, {lat: 80, lon: 170}],
* restrictedExtent: [{lat: -65, lon: -120}, {lat: 65, lon: 120}],
* center: {lat: 45, lon: -111}
* });
* (end)
*/
Expand Down Expand Up @@ -519,17 +518,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);
this.minExtent = new OpenLayers.Bounds(this.minExtent, srs);
}
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
Expand Down Expand Up @@ -1687,20 +1687,32 @@ OpenLayers.Map = OpenLayers.Class({
* Set the map center (and optionally, the zoom level).
*
* Parameters:
* lonlat - {<OpenLayers.LonLat>|Array} The new center location.
* If provided as array, the first value is the x coordinate,
* and the 2nd value is the y coordinate.
* center - {<OpenLayers.LonLat>|Array|Object} The new center location. If
* provided as <OpenLayers.LonLat> or Array, coordinates are expected
* to be in the map's <projection>. If provided as array, the first
* value is the x coordinate, and the 2nd value is the y coordinate. If
* provided as object, latitude and longitude in WGS84 are expected as
* lat and lon properties. See example below.
* zoom - {Integer} Optional zoom level.
* dragging - {Boolean} Specifies whether or not to trigger
* movestart/end events
* forceZoomChange - {Boolean} Specifies whether or not to trigger zoom
* change events (needed on baseLayer change)
*
* On a map with projection set to "EPSG:3857", the following three
* statements are equivalent:
*
* (code)
* map.setCenter({lat: 45, lon: -111});
* map.setCenter([-12356463.476333, 5621521.4854095]);
* map.setCenter(new OpenLayers.LonLat(-12356463.476333, 5621521.4854095));
* (end)
*
* TBD: reconsider forceZoomChange in 3.0
*/
setCenter: function(lonlat, zoom, dragging, forceZoomChange) {
setCenter: function(center, zoom, dragging, forceZoomChange) {
this.panTween && this.panTween.stop();
this.moveTo(lonlat, zoom, {
this.moveTo(center, zoom, {
'dragging': dragging,
'forceZoomChange': forceZoomChange
});
Expand Down Expand Up @@ -1798,7 +1810,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 = {};
Expand Down Expand Up @@ -2309,17 +2321,31 @@ OpenLayers.Map = OpenLayers.Class({
* Zoom to the passed in bounds, recenter
*
* Parameters:
* bounds - {<OpenLayers.Bounds>|Array} If provided as an array, the array
* should consist of four values (left, bottom, right, top).
* bounds - {<OpenLayers.Bounds>|Array}. If provided as an array of four
* values or as <OpenLayers.Bounds>, coordinates are expected to be in
* the map's <projection>. If provided as array of four values, the
* order of the values is left, bottom, right, top. The bounds can
* also be provided as an array of two values. In this case the first
* value is an object literal with lat and lon (WGS84 latitude and
* longitude) for the bottom left corner, and another one for the top
* right corner. See example below.
* closest - {Boolean} Find the zoom level that most closely fits the
* specified bounds. Note that this may result in a zoom that does
* not exactly contain the entire extent.
* Default is false.
*
* On a map with projection set to "EPSG:3857", the following three
* statements are equivalent:
*
* (code)
* map.zoomToExtent({lat: -80, lon: -170}, {lat: 80, lon: 170});
* map.zoomToExtent([-18924313.432222, -15538711.094146, 18924313.432222, 15538711.094146]);
* map.zoomToExtent(new OpenLayers.Bounds(-18924313.432222, -15538711.094146, 18924313.432222, 15538711.094146));
* (end)
*/
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) {
Expand Down
12 changes: 11 additions & 1 deletion tests/BaseTypes/Bounds.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script type="text/javascript">
var bounds;
function test_Bounds_constructor (t) {
t.plan( 26 );
t.plan( 31 );

bounds = new OpenLayers.Bounds();
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
Expand Down Expand Up @@ -53,6 +53,16 @@
t.eq(bounds.right, 180, "(array) right");
t.eq(bounds.top, 90, "(array) top");

// allow construction from a latlon object and transform
bounds = new OpenLayers.Bounds(
[{lon: -180, lat: -85.0511287798066}, {lon: 180, lat: 85.0511287798066}],
new OpenLayers.Projection("EPSG:3857")
);
t.ok(bounds instanceof OpenLayers.Bounds, "(array latlon) correct instance");
t.eq(bounds.left, -20037508.34, "(array latlon) left");
t.eq(bounds.bottom, -20037508.34, "(array latlon) bottom");
t.eq(bounds.right, 20037508.34, "(array latlon) right");
t.eq(bounds.top, 20037508.34, "(array latlon) top");
}

function test_Bounds_constructorFromStrings(t) {
Expand Down
9 changes: 8 additions & 1 deletion tests/BaseTypes/LonLat.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var lonlat;

function test_LonLat_constructor (t) {
t.plan( 8 );
t.plan( 10 );
lonlat = new OpenLayers.LonLat(6, 5);
t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
Expand All @@ -23,6 +23,13 @@
t.eq(lonlat.lon, 1, "lon from array");
t.eq(lonlat.lat, 2, "lat from array");

// allow construction from single latlon arg with transform
lonlat = new OpenLayers.LonLat(
{lon: 180, lat: -85.0511287798066},
new OpenLayers.Projection("EPSG:3857")
);
t.eq(lonlat.lon, 20037508.34, "lon from latlon object");
t.eq(lonlat.lat, -20037508.34, "lat from latlon object");
}

function test_LonLat_constructorFromStrings (t) {
Expand Down
Loading