1 /* Copyright (c) 2006-2009 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Layer/Grid.js
10 * Class: OpenLayers.Layer.XYZ
11 * The XYZ class is designed to make it easier for people who have tiles
12 * arranged by a standard XYZ grid.
14 OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
17 * APIProperty: isBaseLayer
18 * Default is true, as this is designed to be a base tile source.
23 * APIProperty: sphericalMecator
24 * Whether the tile extents should be set to the defaults for
25 * spherical mercator. Useful for things like OpenStreetMap.
26 * Default is false, except for the OSM subclass.
28 sphericalMercator: false,
31 * Constructor: OpenLayers.Layer.OSM
36 * options - {Object} Hashtable of extra options to tag onto the layer
38 initialize: function(name, url, options) {
39 if (options && options.sphericalMercator || this.sphericalMercator) {
40 options = OpenLayers.Util.extend({
41 maxExtent: new OpenLayers.Bounds(
47 maxResolution: 156543.0339,
50 projection: "EPSG:900913"
53 url = url || this.url;
54 name = name || this.name;
55 var newArguments = [name, url, {}, options];
56 OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
61 * Create a clone of this layer
64 * obj - {Object} Is this ever used?
67 * {<OpenLayers.Layer.Grid>} An exact clone of this OpenLayers.Layer.Grid
69 clone: function (obj) {
72 obj = new OpenLayers.Layer.XYZ(this.name,
77 //get all additions from superclasses
78 obj = OpenLayers.Layer.HTTPRequest.prototype.clone.apply(this, [obj]);
80 // copy/set any non-init, non-simple values here
81 if (this.tileSize != null) {
82 obj.tileSize = this.tileSize.clone();
85 // we do not want to copy reference to grid, so we make a new array
95 * bounds - {<OpenLayers.Bounds>}
98 * {String} A string with the layer's url and parameters and also the
99 * passed-in bounds and appropriate tile size specified as
102 getURL: function (bounds) {
103 var res = this.map.getResolution();
104 var x = Math.round((bounds.left - this.maxExtent.left)
105 / (res * this.tileSize.w));
106 var y = Math.round((this.maxExtent.top - bounds.top)
107 / (res * this.tileSize.h));
108 var z = this.map.getZoom();
109 var limit = Math.pow(2, z);
112 var s = '' + x + y + z;
113 if (url instanceof Array)
115 url = this.selectUrl(s, url);
118 var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z});
125 * addTile creates a tile, initializes it, and adds it to the layer div.
128 * bounds - {<OpenLayers.Bounds>}
129 * position - {<OpenLayers.Pixel>}
132 * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image
134 addTile:function(bounds,position) {
135 return new OpenLayers.Tile.Image(this, position, bounds,
136 null, this.tileSize);
140 * When the layer is added to a map, then we can fetch our origin
141 * (if we don't have one.)
144 * map - {<OpenLayers.Map>}
146 setMap: function(map) {
147 OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
148 if (!this.tileOrigin) {
149 this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
150 this.maxExtent.bottom);
154 CLASS_NAME: "OpenLayers.Layer.XYZ"
159 * Class: OpenLayers.Layer.OSM
160 * A class to access OpenStreetMap tiles. By default, uses the OpenStreetMap
161 * hosted tile.openstreetmap.org 'Mapnik' tileset. If you wish to use
162 * tiles@home / osmarender layer instead, you can pass a layer like:
165 * new OpenLayers.Layer.OSM("t@h",
166 * "http://tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png");
169 * This layer defaults to Spherical Mercator.
172 OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
173 name: "OpenStreetMap",
174 attribution: "Data CC-By-SA by <a href='http://openstreetmap.org/'>OpenStreetMap</a>",
175 sphericalMercator: true,
176 url: 'http://tile.openstreetmap.org/${z}/${x}/${y}.png',
177 CLASS_NAME: "OpenLayers.Layer.OSM"