1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Layer/Grid.js |
---|
8 | * @requires OpenLayers/Tile/Image.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Layer.XYZ |
---|
13 | * The XYZ class is designed to make it easier for people who have tiles |
---|
14 | * arranged by a standard XYZ grid. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Layer.Grid> |
---|
18 | */ |
---|
19 | OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIProperty: isBaseLayer |
---|
23 | * Default is true, as this is designed to be a base tile source. |
---|
24 | */ |
---|
25 | isBaseLayer: true, |
---|
26 | |
---|
27 | /** |
---|
28 | * APIProperty: sphericalMecator |
---|
29 | * Whether the tile extents should be set to the defaults for |
---|
30 | * spherical mercator. Useful for things like OpenStreetMap. |
---|
31 | * Default is false, except for the OSM subclass. |
---|
32 | */ |
---|
33 | sphericalMercator: false, |
---|
34 | |
---|
35 | /** |
---|
36 | * APIProperty: zoomOffset |
---|
37 | * {Number} If your cache has more zoom levels than you want to provide |
---|
38 | * access to with this layer, supply a zoomOffset. This zoom offset |
---|
39 | * is added to the current map zoom level to determine the level |
---|
40 | * for a requested tile. For example, if you supply a zoomOffset |
---|
41 | * of 3, when the map is at the zoom 0, tiles will be requested from |
---|
42 | * level 3 of your cache. Default is 0 (assumes cache level and map |
---|
43 | * zoom are equivalent). |
---|
44 | */ |
---|
45 | zoomOffset: 0, |
---|
46 | |
---|
47 | /** |
---|
48 | * Constructor: OpenLayers.Layer.XYZ |
---|
49 | * |
---|
50 | * Parameters: |
---|
51 | * name - {String} |
---|
52 | * url - {String} |
---|
53 | * options - {Object} Hashtable of extra options to tag onto the layer |
---|
54 | */ |
---|
55 | initialize: function(name, url, options) { |
---|
56 | if (options && options.sphericalMercator || this.sphericalMercator) { |
---|
57 | options = OpenLayers.Util.extend({ |
---|
58 | maxExtent: new OpenLayers.Bounds( |
---|
59 | -128 * 156543.0339, |
---|
60 | -128 * 156543.0339, |
---|
61 | 128 * 156543.0339, |
---|
62 | 128 * 156543.0339 |
---|
63 | ), |
---|
64 | maxResolution: 156543.0339, |
---|
65 | numZoomLevels: 19, |
---|
66 | units: "m", |
---|
67 | projection: "EPSG:900913" |
---|
68 | }, options); |
---|
69 | } |
---|
70 | url = url || this.url; |
---|
71 | name = name || this.name; |
---|
72 | var newArguments = [name, url, {}, options]; |
---|
73 | OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments); |
---|
74 | }, |
---|
75 | |
---|
76 | /** |
---|
77 | * APIMethod: clone |
---|
78 | * Create a clone of this layer |
---|
79 | * |
---|
80 | * Parameters: |
---|
81 | * obj - {Object} Is this ever used? |
---|
82 | * |
---|
83 | * Returns: |
---|
84 | * {<OpenLayers.Layer.XYZ>} An exact clone of this OpenLayers.Layer.XYZ |
---|
85 | */ |
---|
86 | clone: function (obj) { |
---|
87 | |
---|
88 | if (obj == null) { |
---|
89 | obj = new OpenLayers.Layer.XYZ(this.name, |
---|
90 | this.url, |
---|
91 | this.getOptions()); |
---|
92 | } |
---|
93 | |
---|
94 | //get all additions from superclasses |
---|
95 | obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); |
---|
96 | |
---|
97 | return obj; |
---|
98 | }, |
---|
99 | |
---|
100 | /** |
---|
101 | * Method: getUrl |
---|
102 | * |
---|
103 | * Parameters: |
---|
104 | * bounds - {<OpenLayers.Bounds>} |
---|
105 | * |
---|
106 | * Returns: |
---|
107 | * {String} A string with the layer's url and parameters and also the |
---|
108 | * passed-in bounds and appropriate tile size specified as |
---|
109 | * parameters |
---|
110 | */ |
---|
111 | getURL: function (bounds) { |
---|
112 | var res = this.map.getResolution(); |
---|
113 | var x = Math.round((bounds.left - this.maxExtent.left) |
---|
114 | / (res * this.tileSize.w)); |
---|
115 | var y = Math.round((this.maxExtent.top - bounds.top) |
---|
116 | / (res * this.tileSize.h)); |
---|
117 | var z = this.map.getZoom() + this.zoomOffset; |
---|
118 | |
---|
119 | var url = this.url; |
---|
120 | var s = '' + x + y + z; |
---|
121 | if (url instanceof Array) |
---|
122 | { |
---|
123 | url = this.selectUrl(s, url); |
---|
124 | } |
---|
125 | |
---|
126 | var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z}); |
---|
127 | |
---|
128 | return path; |
---|
129 | }, |
---|
130 | |
---|
131 | /** |
---|
132 | * Method: addTile |
---|
133 | * addTile creates a tile, initializes it, and adds it to the layer div. |
---|
134 | * |
---|
135 | * Parameters: |
---|
136 | * bounds - {<OpenLayers.Bounds>} |
---|
137 | * position - {<OpenLayers.Pixel>} |
---|
138 | * |
---|
139 | * Returns: |
---|
140 | * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image |
---|
141 | */ |
---|
142 | addTile:function(bounds,position) { |
---|
143 | return new OpenLayers.Tile.Image(this, position, bounds, |
---|
144 | null, this.tileSize); |
---|
145 | }, |
---|
146 | |
---|
147 | /* APIMethod: setMap |
---|
148 | * When the layer is added to a map, then we can fetch our origin |
---|
149 | * (if we don't have one.) |
---|
150 | * |
---|
151 | * Parameters: |
---|
152 | * map - {<OpenLayers.Map>} |
---|
153 | */ |
---|
154 | setMap: function(map) { |
---|
155 | OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments); |
---|
156 | if (!this.tileOrigin) { |
---|
157 | this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left, |
---|
158 | this.maxExtent.bottom); |
---|
159 | } |
---|
160 | }, |
---|
161 | |
---|
162 | CLASS_NAME: "OpenLayers.Layer.XYZ" |
---|
163 | }); |
---|
164 | |
---|
165 | |
---|
166 | /** |
---|
167 | * Class: OpenLayers.Layer.OSM |
---|
168 | * A class to access OpenStreetMap tiles. By default, uses the OpenStreetMap |
---|
169 | * hosted tile.openstreetmap.org 'Mapnik' tileset. If you wish to use |
---|
170 | * tiles@home / osmarender layer instead, you can pass a layer like: |
---|
171 | * |
---|
172 | * (code) |
---|
173 | * new OpenLayers.Layer.OSM("t@h", |
---|
174 | * "http://tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png"); |
---|
175 | * (end) |
---|
176 | * |
---|
177 | * This layer defaults to Spherical Mercator. |
---|
178 | * |
---|
179 | * Inherits from: |
---|
180 | * - <OpenLayers.Layer.XYZ> |
---|
181 | */ |
---|
182 | OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.XYZ, { |
---|
183 | name: "OpenStreetMap", |
---|
184 | attribution: "Data CC-By-SA by <a href='http://openstreetmap.org/'>OpenStreetMap</a>", |
---|
185 | sphericalMercator: true, |
---|
186 | url: 'http://tile.openstreetmap.org/${z}/${x}/${y}.png', |
---|
187 | clone: function(obj) { |
---|
188 | if (obj == null) { |
---|
189 | obj = new OpenLayers.Layer.OSM( |
---|
190 | this.name, this.url, this.getOptions()); |
---|
191 | } |
---|
192 | obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]); |
---|
193 | return obj; |
---|
194 | }, |
---|
195 | CLASS_NAME: "OpenLayers.Layer.OSM" |
---|
196 | }); |
---|