Skip to content

Instantly share code, notes, and snippets.

@ns-1m
Created May 2, 2012 03:30
Show Gist options
  • Save ns-1m/2573378 to your computer and use it in GitHub Desktop.
Save ns-1m/2573378 to your computer and use it in GitHub Desktop.
Snippets of OpenLayers.Layer.TMS Offline
tmsLayer = new OpenLayers.Layer.TMS(lay.name,
'Content/outputs/', {
type: 'png',
alpha: true,
serviceVersion: '',
layername: 'Magnetics',
tileExtent: new OpenLayers.Bounds(lay.imageBounds.left,
lay.imageBounds.bottom, lay.imageBounds.right,
lay.imageBounds.top).transform(dproj, proj),
mapMinZoom: lay.imageMinZoom,
mapMaxZoom: lay.imageMaxZoom,
getURL: tms_getTileURL,
displayOutsideMaxExtent: true,
zoomOffset: 0,
isBaseLayer: lay.baselayer
});
# the getURL to use is:
function tms_getTileURL(bounds) {
adjustBounds(bounds, this.maxExtent);
var res = this.map.getResolution();
var x = Math.round((bounds.left - this.maxExtent.left) / (res *
this.tileSize.w));
var y = Math.round((bounds.bottom - this.tileOrigin.lat) / (res *
this.tileSize.h));
var z = this.map.getZoom();
z = this.serverResolutions != null ?
OpenLayers.Util.indexOf(this.serverResolutions, res) : z + this.zoomOffset;
if (this.tileExtent.left > this.tileExtent.right) {
var b1 = new OpenLayers.Bounds(this.tileExtent.left,
this.tileExtent.bottom, this.map.maxExtent.right, this.tileExtent.top);
var b2 = new OpenLayers.Bounds(this.map.maxExtent.left,
this.tileExtent.bottom, this.tileExtent.right, this.tileExtent.top);
if ((b1.intersectsBounds(bounds) ||
b2.intersectsBounds(bounds)) && z >= this.mapMinZoom && z <=
this.mapMaxZoom) {
return this.url + this.serviceVersion + "/" +
this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;
} else {
return "images/none.png";
}
} else {
if (this.tileExtent.intersectsBounds(bounds) && z >=
this.mapMinZoom && z <= this.mapMaxZoom) {
return this.url + this.serviceVersion + "/" +
this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;
} else {
return "images/none.png";
}
}
}
####################################
new OpenLayers.Layer.TMS(lay.name,
'openlayers/', {
type: 'png',
alpha: true,
serviceVersion: '',
layername: 'naturalearth',
tileExtent: new OpenLayers.Bounds(lay.imageBounds.left,
lay.imageBounds.bottom, lay.imageBounds.right,
lay.imageBounds.top).transform(gg, sm),
mapMinZoom: lay.imageMinZoom,
mapMaxZoom: lay.imageMaxZoom,
getURL: tms_getTileURL,
displayOutsideMaxExtent: true,
zoomOffset: 0,
isBaseLayer: lay.baselayer
}),
new OpenLayers.Layer.TMS("MapBox Tile Natural Earth 2",
'http://a.tiles.mapbox.com/mapbox/',
{layername: 'natural-earth-2',
type: 'png',
transitionEffect: 'resize'
}),
#################################################
tmsLayer = new OpenLayers.Layer.TMS(lay.name,
'Content/outputs/', {
type: 'png',
alpha: true,
serviceVersion: '',
layername: 'Magnetics',
tileExtent: new OpenLayers.Bounds(lay.imageBounds.left,
lay.imageBounds.bottom, lay.imageBounds.right,
lay.imageBounds.top).transform(dproj, proj),
mapMinZoom: lay.imageMinZoom,
mapMaxZoom: lay.imageMaxZoom,
getURL: tms_getTileURL,
displayOutsideMaxExtent: true,
zoomOffset: 0,
isBaseLayer: lay.baselayer
});
#the getURL to use is:
function tms_getTileURL(bounds) {
adjustBounds(bounds, this.maxExtent);
var res = this.map.getResolution();
var x = Math.round((bounds.left - this.maxExtent.left) / (res *
this.tileSize.w));
var y = Math.round((bounds.bottom - this.tileOrigin.lat) / (res *
this.tileSize.h));
var z = this.map.getZoom();
z = this.serverResolutions != null ?
OpenLayers.Util.indexOf(this.serverResolutions, res) : z + this.zoomOffset;
if (this.tileExtent.left > this.tileExtent.right) {
var b1 = new OpenLayers.Bounds(this.tileExtent.left,
this.tileExtent.bottom, this.map.maxExtent.right, this.tileExtent.top);
var b2 = new OpenLayers.Bounds(this.map.maxExtent.left,
this.tileExtent.bottom, this.tileExtent.right, this.tileExtent.top);
if ((b1.intersectsBounds(bounds) ||
b2.intersectsBounds(bounds)) && z >= this.mapMinZoom && z <=
this.mapMaxZoom) {
return this.url + this.serviceVersion + "/" +
this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;
} else {
return "images/none.png";
}
} else {
if (this.tileExtent.intersectsBounds(bounds) && z >=
this.mapMinZoom && z <= this.mapMaxZoom) {
return this.url + this.serviceVersion + "/" +
this.layername + "/" + z + "/" + x + "/" + y + "." + this.type;
} else {
return "images/none.png";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment