OpenLayers加载本地ArcServer切片地图
使用水经注下载的地图,切为ArcServer格式,在XYZ类的基础上扩展Grid。
/**
* @requires OpenLayers/Layer/Grid.js
*/
/**
* Class: OpenLayers.Layer.ArcTileXYZ
* The ArcTileXYZ class is designed to make it easier for people who have tiles
* arranged by a standard XYZ grid.
*
* Inherits from:
* -
*/
OpenLayers.Layer.ArcTileXYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
/**
* APIProperty: isBaseLayer
* Default is true, as this is designed to be a base tile source.
*/
isBaseLayer: true,
/**
* APIProperty: sphericalMercator
* Whether the tile extents should be set to the defaults for
* spherical mercator. Useful for things like OpenStreetMap.
* Default is false, except for the OSM subclass.
*/
sphericalMercator: false,
/**
* APIProperty: zoomOffset
* {Number} If your cache has more zoom levels than you want to provide
* access to with this layer, supply a zoomOffset. This zoom offset
* is added to the current map zoom level to determine the level
* for a requested tile. For example, if you supply a zoomOffset
* of 3, when the map is at the zoom 0, tiles will be requested from
* level 3 of your cache. Default is 0 (assumes cache level and map
* zoom are equivalent). Using is an alternative to
* setting if you only want to expose a subset
* of the server resolutions.
*/
zoomOffset: 0,
/**
* APIProperty: serverResolutions
* {Array} A list of all resolutions available on the server. Only set this
* property if the map resolutions differ from the server. This
* property serves two purposes. (a) can include
* resolutions that the server supports and that you don't want to
* provide with this layer; you can also look at , which is
* an alternative to for that specific purpose.
* (b) The map can work with resolutions that aren't supported by
* the server, i.e. that aren't in . When the
* map is displayed in such a resolution data for the closest
* server-supported resolution is loaded and the layer div is
* stretched as necessary.
*/
serverResolutions: null,
/**
* Constructor: OpenLayers.Layer.ArcTileXYZ
*
* Parameters:
* name - {String}
* url - {String}
* options - {Object} Hashtable of extra options to tag onto the layer
*/
initialize: function (name, url, options) {
if (options && options.sphericalMercator || this.sphericalMercator) {
options = OpenLayers.Util.extend({
projection: "EPSG:900913",
numZoomLevels: 19
}, options);
}
OpenLayers.Layer.Grid.prototype.initialize.apply(this, [
name || this.name, url || this.url, {}, options
]);
},
/**
* APIMethod: clone
* Create a clone of this layer
*
* Parameters:
* obj - {Object} Is this ever used?
*
* Returns:
* {} An exact clone of this OpenLayers.Layer.ArcTileXYZ
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.ArcTileXYZ(this.name,
this.url,
this.getOptions());
}
//get all additions from superclasses
obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
return obj;
},
/**
* Method: getURL
*
* Parameters:
* bounds - {}
*
* Returns:
* {String} A string with the layer's url and parameters and also the
* passed-in bounds and appropriate tile size specified as
* parameters
*/
getURL: function (bounds) {
var xyz = this.getXYZ(bounds);
var url = this.url;
if (OpenLayers.Util.isArray(url)) {
var s = '' + xyz.x + xyz.y + xyz.z;
url = this.selectUrl(s, url);
}
return OpenLayers.String.format(url, xyz);
},
/**
* Method: getXYZ
* Calculates x, y and z for the given bounds.
*
* Parameters:
* bounds - {}
*
* Returns:
* {Object} - an object with x, y and z properties.
*/
getXYZ: function (bounds) {
var res = this.getServerResolution();
var x = Math.round((bounds.left - this.maxExtent.left) /
(res * this.tileSize.w));
var y = Math.round((this.maxExtent.top - bounds.top) /
(res * this.tileSize.h));
var z = this.getServerZoom();
if (this.wrapDateLine) {
var limit = Math.pow(2, z);
x = ((x % limit) + limit) % limit;
}
return { 'x': this.getX(x), 'y': this.getY(y), 'z': this.getZ(z) };
},
/* APIMethod: setMap
* When the layer is added to a map, then we can fetch our origin
* (if we don't have one.)
*
* Parameters:
* map - {}
*/
setMap: function (map) {
OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
if (!this.tileOrigin) {
this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
this.maxExtent.bottom);
}
},
getZ: function (z) {
return this.format(z, 2, 10);
},
getY: function (y) {
if (y === -1) {
y = 0;
}
return this.format(y, 8, 16);
},
getX: function (x) {
return this.format(x, 8, 16);
},
format: function (num, n, s) {
var d = Array(n).join(0) + num.toString(s).toUpperCase();
return d.slice(-n);
},
CLASS_NAME: "OpenLayers.Layer.ArcTileXYZ"
});
使用:
var baseLayer = new OpenLayers.Layer.ArcTileXYZ('底图',//name,url,options
'http://192.168.0.102:6068/Map/L${z}/R${y}/C${x}.png',
{
isBaseLayer: true,
visibility: true,
displayInLayerSwitcher: false
});
map.addLayer(baseLayer);
////////////////////////////
后来网上搜了一个类似的http://blog.csdn.net/devcopper/article/details/39479539