-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
map.js
298 lines (268 loc) · 10.2 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
(function(exports) {
/*
* tile.stamen.js v1.3.0
*/
/*
Geomap Background Creator
Local Base Codes
*/
var SUBDOMAINS = "a. b. c. d.".split(" "),
MAKE_PROVIDER = function(layer, type, minZoom, maxZoom) {
return {
"url": ["//stamen-tiles-{S}a.ssl.fastly.net/", layer, "/{Z}/{X}/{Y}.", type].join(""),
"type": type,
"subdomains": SUBDOMAINS.slice(),
"minZoom": minZoom,
"maxZoom": maxZoom,
"attribution": [
'Map tiles by <a href="http://stamen.com/">Stamen Design</a>, ',
'under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. ',
'Data by <a href="http://openstreetmap.org/">OpenStreetMap</a>, ',
'under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.'
].join("")
};
},
PROVIDERS = {
"toner": MAKE_PROVIDER("toner", "png", 0, 20),
"terrain": MAKE_PROVIDER("terrain", "png", 0, 18),
"terrain-classic": MAKE_PROVIDER("terrain-classic", "png", 0, 18),
"watercolor": MAKE_PROVIDER("watercolor", "jpg", 1, 18),
"trees-cabs-crime": {
"url": "http://{S}.tiles.mapbox.com/v3/stamen.trees-cabs-crime/{Z}/{X}/{Y}.png",
"type": "png",
"subdomains": "a b c d".split(" "),
"minZoom": 11,
"maxZoom": 18,
"extent": [
{"lat": 37.853, "lon": -122.577},
{"lat": 37.684, "lon": -122.313}
],
"attribution": [
'Design by Shawn Allen at <a href="http://stamen.com/">Stamen</a>.',
'Data courtesy of <a href="http://fuf.net/">FuF</a>,',
'<a href="http://www.yellowcabsf.com/">Yellow Cab</a>',
'& <a href="http://sf-police.org/">SFPD</a>.'
].join(" ")
}
};
PROVIDERS["terrain-classic"].url = "//stamen-tiles-{S}a.ssl.fastly.net/terrain/{Z}/{X}/{Y}.png";
// set up toner and terrain flavors
setupFlavors("toner", ["hybrid", "labels", "lines", "background", "lite"]);
setupFlavors("terrain", ["background", "labels", "lines"]);
// toner 2010
deprecate("toner", ["2010"]);
// toner 2011 flavors
deprecate("toner", ["2011", "2011-lines", "2011-labels", "2011-lite"]);
var odbl = [
"toner",
"toner-hybrid",
"toner-labels",
"toner-lines",
"toner-background",
"toner-lite",
"terrain",
"terrain-background",
"terrain-lines",
"terrain-labels",
"terrain-classic"
];
for (var i = 0; i < odbl.length; i++) {
var key = odbl[i];
PROVIDERS[key].retina = true;
PROVIDERS[key].attribution = [
'Map tiles by <a href="http://stamen.com/">Stamen Design</a>, ',
'under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. ',
'Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, ',
'under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.'
].join("");
}
/*
* Export stamen.tile to the provided namespace.
*/
exports.stamen = exports.stamen || {};
exports.stamen.tile = exports.stamen.tile || {};
exports.stamen.tile.providers = PROVIDERS;
exports.stamen.tile.getProvider = getProvider;
function deprecate(base, flavors) {
var provider = getProvider(base);
for (var i = 0; i < flavors.length; i++) {
var flavor = [base, flavors[i]].join("-");
PROVIDERS[flavor] = MAKE_PROVIDER(flavor, provider.type, provider.minZoom, provider.maxZoom);
PROVIDERS[flavor].deprecated = true;
}
};
/*
* A shortcut for specifying "flavors" of a style, which are assumed to have the
* same type and zoom range.
*/
function setupFlavors(base, flavors, type) {
var provider = getProvider(base);
for (var i = 0; i < flavors.length; i++) {
var flavor = [base, flavors[i]].join("-");
PROVIDERS[flavor] = MAKE_PROVIDER(flavor, type || provider.type, provider.minZoom, provider.maxZoom);
}
}
/*
* Get the named provider, or throw an exception if it doesn't exist.
*/
function getProvider(name) {
if (name in PROVIDERS) {
var provider = PROVIDERS[name];
if (provider.deprecated && console && console.warn) {
console.warn(name + " is a deprecated style; it will be redirected to its replacement. For performance improvements, please change your reference.");
}
return provider;
} else {
throw 'No such provider (' + name + ')';
}
}
/*
* StamenTileLayer for modestmaps-js
* <https://github.com/modestmaps/modestmaps-js/>
*
* Works with both 1.x and 2.x by checking for the existence of MM.Template.
*/
if (typeof MM === "object") {
var ModestTemplate = (typeof MM.Template === "function")
? MM.Template
: MM.TemplatedMapProvider;
MM.StamenTileLayer = function(name) {
var provider = getProvider(name);
this._provider = provider;
MM.Layer.call(this, new ModestTemplate(provider.url, provider.subdomains));
this.provider.setZoomRange(provider.minZoom, provider.maxZoom);
this.attribution = provider.attribution;
};
MM.StamenTileLayer.prototype = {
setCoordLimits: function(map) {
var provider = this._provider;
if (provider.extent) {
map.coordLimits = [
map.locationCoordinate(provider.extent[0]).zoomTo(provider.minZoom),
map.locationCoordinate(provider.extent[1]).zoomTo(provider.maxZoom)
];
return true;
} else {
return false;
}
}
};
MM.extend(MM.StamenTileLayer, MM.Layer);
}
/*
* StamenTileLayer for Leaflet
* <http://leaflet.cloudmade.com/>
*
* Tested with version 0.3 and 0.4, but should work on all 0.x releases.
*/
if (typeof L === "object") {
L.StamenTileLayer = L.TileLayer.extend({
initialize: function(name, options) {
var provider = getProvider(name),
url = provider.url.replace(/({[A-Z]})/g, function(s) {
return s.toLowerCase();
}),
opts = L.Util.extend({}, options, {
"minZoom": provider.minZoom,
"maxZoom": provider.maxZoom,
"subdomains": provider.subdomains,
"scheme": "xyz",
"attribution": provider.attribution,
sa_id: name
});
L.TileLayer.prototype.initialize.call(this, url, opts);
}
});
/*
* Factory function for consistency with Leaflet conventions
*/
L.stamenTileLayer = function (options, source) {
return new L.StamenTileLayer(options, source);
};
}
/*
* StamenTileLayer for OpenLayers
* <http://openlayers.org/>
*
* Tested with v2.1x.
*/
if (typeof OpenLayers === "object") {
// make a tile URL template OpenLayers-compatible
function openlayerize(url) {
return url.replace(/({.})/g, function(v) {
return "$" + v.toLowerCase();
});
}
// based on http://www.bostongis.com/PrinterFriendly.aspx?content_name=using_custom_osm_tiles
OpenLayers.Layer.Stamen = OpenLayers.Class(OpenLayers.Layer.OSM, {
initialize: function(name, options) {
var provider = getProvider(name),
url = provider.url,
subdomains = provider.subdomains,
hosts = [];
if (url.indexOf("{S}") > -1) {
for (var i = 0; i < subdomains.length; i++) {
hosts.push(openlayerize(url.replace("{S}", subdomains[i])));
}
} else {
hosts.push(openlayerize(url));
}
options = OpenLayers.Util.extend({
"numZoomLevels": provider.maxZoom,
"buffer": 0,
"transitionEffect": "resize",
// see: <http://dev.openlayers.org/apidocs/files/OpenLayers/Layer/OSM-js.html#OpenLayers.Layer.OSM.tileOptions>
// and: <http://dev.openlayers.org/apidocs/files/OpenLayers/Tile/Image-js.html#OpenLayers.Tile.Image.crossOriginKeyword>
"tileOptions": {
"crossOriginKeyword": null
},
"attribution": provider.attribution
}, options);
return OpenLayers.Layer.OSM.prototype.initialize.call(this, name, hosts, options);
}
});
}
/*
* StamenMapType for Google Maps API V3
* <https://developers.google.com/maps/documentation/javascript/>
*/
if (typeof google === "object" && typeof google.maps === "object") {
// Extending Google class based on a post by Bogart Salzberg of Portland Webworks,
// http://www.portlandwebworks.com/blog/extending-googlemapsmap-object
google.maps.ImageMapType = (function(_constructor){
var f = function() {
if (!arguments.length) {
return;
}
_constructor.apply(this, arguments);
}
f.prototype = _constructor.prototype;
return f;
})(google.maps.ImageMapType);
google.maps.StamenMapType = function(name) {
var provider = getProvider(name),
subdomains = provider.subdomains;
return google.maps.ImageMapType.call(this, {
"getTileUrl": function(coord, zoom) {
var numTiles = 1 << zoom,
wx = coord.x % numTiles,
x = (wx < 0) ? wx + numTiles : wx,
y = coord.y,
index = (zoom + x + y) % subdomains.length;
return provider.url
.replace("{S}", subdomains[index])
.replace("{Z}", zoom)
.replace("{X}", x)
.replace("{Y}", y);
},
"tileSize": new google.maps.Size(256, 256),
"name": name,
"minZoom": provider.minZoom,
"maxZoom": provider.maxZoom
});
};
// FIXME: is there a better way to extend classes in Google land?
// Possibly fixed, see above ^^^ | SC
google.maps.StamenMapType.prototype = new google.maps.ImageMapType;
}
})(typeof exports === "undefined" ? this : exports);