Files
openlayers/lib/OpenLayers/Layer/HTTPRequest.js
crschmidt 6efaf328e5 Commit scale-based methods back into trunk. Layers or Maps now support setting
a list of scales or resolutions in the options to the constructor, from which
resolutions are calculated. Map now has a 'setScale' function which will allow
you to zoom to as close to a given scale as possible.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@1171 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2006-08-09 19:14:16 +00:00

179 lines
5.7 KiB
JavaScript

/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
* text of the license. */
// @require: OpenLayers/Layer.js
// @require: OpenLayers/Util.js
/**
* @class
*/
OpenLayers.Layer.HTTPRequest = Class.create();
OpenLayers.Layer.HTTPRequest.prototype =
Object.extend( new OpenLayers.Layer(), {
/** @type String */
url: null,
/** Hashtable of key/value parameters
* @type Object */
params: null,
/**
* @constructor
*
* @param {String} name
* @param {String} url
* @param {Object} params
* @param {Object} options Hashtable of extra options to tag onto the layer
*/
initialize: function(name, url, params, options) {
var newArguments = arguments;
if (arguments.length > 0) {
newArguments = [name, options];
}
OpenLayers.Layer.prototype.initialize.apply(this, newArguments);
this.url = url;
this.params = Object.extend( new Object(), params);
},
setMap: function(map) {
OpenLayers.Layer.prototype.setMap.apply(this, arguments);
this.initResolutions();
},
/**
*
*/
destroy: function() {
this.url = null;
this.params = null;
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
/**
* @param {Object} obj
*
* @returns An exact clone of this OpenLayers.Layer.HTTPRequest
* @type OpenLayers.Layer.HTTPRequest
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.HTTPRequest(this.name,
this.url,
this.params,
this.options);
}
//get all additions from superclasses
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
return obj;
},
/**
* @param {String} newUrl
*/
setUrl: function(newUrl) {
this.url = newUrl;
},
/** Deprecated wrapper for mergeNewParams() just so as to not break
* anyone's code who might be using this
*
* @deprecated
* @param {Object} newParams
*/
changeParams:function(newParams) {
this.mergeNewParams(newParams);
},
/**
* @param {Object} newParams
*/
mergeNewParams:function(newParams) {
this.params = Object.extend(this.params, newParams);
},
/** combine url with layer's params and these newParams.
*
* does checking on the serverPath variable, allowing for cases when it
* is supplied with trailing ? or &, as well as cases where not.
*
* return in formatted string like this:
* "server?key1=value1&key2=value2&key3=value3"
*
* @param {Object} newParams
* @param {String} altUrl Use this as the url instead of the layer's url
*
* @type String
*/
getFullRequestString:function(newParams, altUrl) {
// use layer's url unless altUrl passed in
var url = (altUrl == null) ? this.url : altUrl;
// requestString always starts with url
var requestString = url;
// create a new params hashtable with all the layer params and the
// new params together. then convert to string
var allParams = Object.extend(new Object(), this.params);
var allParams = Object.extend(allParams, newParams);
var paramsString = OpenLayers.Util.getParameterString(allParams);
if (paramsString != "") {
var lastServerChar = this.url.charAt(this.url.length - 1);
if ((lastServerChar == "&") || (lastServerChar == "?")) {
requestString += paramsString;
} else {
if (this.url.indexOf('?') == -1) {
//serverPath has no ? -- add one
requestString += '?' + paramsString;
} else {
//serverPath contains ?, so must already have paramsString at the end
requestString += '&' + paramsString;
}
}
}
return requestString;
},
initResolutions: function() {
if (this.scales != null) {
this.resolutions = new Array();
for(var i = 0; i < this.scales.length; i++)
this.resolutions[i] = OpenLayers.Util.getResolutionFromScale(this.scales[i], this.units);
this.maxZoomLevel = this.resolutions.length;
} else if (this.resolutions != null) {
this.maxZoomLevel = this.resolutions.length;
} else {
this.resolutions = new Array();
if (this.minScale)
this.maxResolution = OpenLayers.Util.getResolutionFromScale(this.minScale, this.units);
var maxRes = this.getMaxResolution();
if (this.maxScale) {
/* This will cause this.map.getMaxZoomLevel() to be set the next time
* it is called, which means that the next portion here will succeed. */
var minRes = OpenLayers.Util.getResolutionFromScale(this.maxScale);
this.maxZoomLevel = Math.floor(Math.log(maxRes/minRes) / Math.log(2));
}
for (var i=this.getMinZoomLevel(); i <= this.getMaxZoomLevel(); i++) {
this.resolutions.push(maxRes / Math.pow(2, i));
}
}
},
getResolution: function() {
var zoom = this.map.getZoom();
return this.resolutions[zoom];
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
});