GoogleNG.js
11.5 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Layer/XYZ.js
* @requires OpenLayers/Tile/Google.js
* @requires OpenLayers/Layer/SphericalMercator.js
*/
/**
* Class: OpenLayers.Layer.GoogleNG
* Google layer using <OpenLayers.Tile.Google> tiles.
*
* Inherits from:
* - <OpenLayers.Layer.XYZ>
*/
OpenLayers.Layer.GoogleNG = OpenLayers.Class(OpenLayers.Layer.XYZ, {
/**
* Property: SUPPORTED_TRANSITIONS
* {Array} An immutable (that means don't change it!) list of supported
* transitionEffect values. This layer type supports none.
*/
SUPPORTED_TRANSITIONS: [],
/**
* Property: serverResolutions
* {Array} the resolutions provided by the Google API.
*/
serverResolutions: [
156543.03390625, 78271.516953125, 39135.7584765625,
19567.87923828125, 9783.939619140625, 4891.9698095703125,
2445.9849047851562, 1222.9924523925781, 611.4962261962891,
305.74811309814453, 152.87405654907226, 76.43702827453613,
38.218514137268066, 19.109257068634033, 9.554628534317017,
4.777314267158508, 2.388657133579254, 1.194328566789627,
0.5971642833948135, 0.29858214169740677, 0.14929107084870338,
0.07464553542435169, 0.037322767712175846
],
/**
* Property: attributionTemplate
* {String}
*/
attributionTemplate: '<span class="olGoogleAttribution ${mapType}">' +
'<div><a title="Click to see this area on Google Maps" ' +
'target="_blank" href="http://maps.google.com/maps?' +
'll=${center}&z=${zoom}&t=${t}"><img width="62" height="24" ' +
'src="http://maps.gstatic.com/mapfiles/google_white.png"/></a>' +
'</div>${mapData}<a style="white-space: nowrap" target="_blank" ' +
'href="http://www.google.com/help/terms_maps.html">' +
'Terms of Use</a></span>',
/**
* Property: mapTypes
* {Object} mapping of {google.maps.MapTypeId} to the t param of
* http://maps.google.com/maps? permalinks
*/
mapTypes: {
"roadmap": "m",
"satellite": "k",
"hybrid": "h",
"terrain": "p"
},
/**
* APIProperty: type
* {google.maps.MapTypeId} See
* http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId
*/
type: null,
/**
* Constructor: OpenLayers.Layer.GoogleNG
* Create a new GoogleNG layer. Requires the GMaps v3 JavaScript API script
* (http://maps.google.com/maps/api/js?v=3.5&sensor=false) loaded in
* the html document. Note: Terms of Service compliant use requires the map
* to be configured with an <OpenLayers.Control.Attribution> control and
* the attribution placed on the map.
*
* Example:
* (code)
* var terrain = new OpenLayers.Layer.GoogleNG({
* name: "Google Terrain",
* type: google.maps.MapTypeId.TERRAIN
* });
* (end)
*
* Parameters:
* options - {Object} Configuration properties for the layer.
*
* Required configuration properties:
* type - {google.maps.MapTypeId} The layer identifier. See
* http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId
* for valid types.
*
* Any other documented layer properties can be provided in the config object.
*/
initialize: function(options) {
options = OpenLayers.Util.applyDefaults({
sphericalMercator: true
}, options);
if (!options.type) {
options.type = google.maps.MapTypeId.ROADMAP;
}
var newArgs = [options.name, null, options];
OpenLayers.Layer.XYZ.prototype.initialize.apply(this, newArgs);
if (!OpenLayers.Layer.GoogleNG.mapObject) {
OpenLayers.Layer.GoogleNG.mapObject =
new google.maps.Map(document.createElement("div"));
}
if (OpenLayers.Layer.GoogleNG.mapObject.mapTypes[this.type]) {
this.initLayer();
} else {
google.maps.event.addListenerOnce(
OpenLayers.Layer.GoogleNG.mapObject,
"idle",
OpenLayers.Function.bind(this.initLayer, this)
);
}
},
/**
* Method: initLayer
*
* Sets layer properties according to the metadata provided by the API
*/
initLayer: function() {
var mapType = OpenLayers.Layer.GoogleNG.mapObject.mapTypes[this.type];
if (!this.name) {
this.setName("Google " + mapType.name);
}
var minZoom = mapType.minZoom || 0;
this.addOptions({
maxResolution: Math.min(
this.serverResolutions[minZoom], this.maxResolution
),
zoomOffset: minZoom,
numZoomLevels: Math.min(
mapType.maxZoom + 1 - minZoom, this.numZoomLevels
)
}, true);
},
/**
* Method: addTile
* Create a tile, initialize it, and add it to the layer div.
*
* Parameters
* bounds - {<OpenLayers.Bounds>}
* position - {<OpenLayers.Pixel>}
*
* Returns:
* {<OpenLayers.Tile.Google>} The added OpenLayers.Tile.Google
*/
addTile:function(bounds, position) {
return new OpenLayers.Tile.Google(
this, position, bounds, this.tileOptions
);
},
/**
* Method: updateAttribution
* Updates the attribution using the <attributionTemplate>
*
* Parameters:
* copyrights - {Object} Object with "m", "k", "h" and "p" properties (see
* <mapTypes>), each holding an array of copyrights.
*/
updateAttribution: function(copyrights) {
var myCopyrights;
if (this.type == google.maps.MapTypeId.HYBRID) {
// the Copyright Service returns "k" and "m" copyrights for the
// HYBRID layer type.
var candidates = [].concat(
copyrights["h"], copyrights["k"], copyrights["m"]
);
myCopyrights = [];
for (var i=candidates.length-1; i>=0; --i) {
if (OpenLayers.Util.indexOf(candidates, myCopyrights) == -1) {
myCopyrights.push(candidates[i]);
}
}
} else {
myCopyrights = copyrights[this.mapTypes[this.type]];
}
var mapData = myCopyrights.length == 0 ? "" :
"Map Data ©" + new Date().getFullYear() + " " +
myCopyrights.join(", ") + " - ";
var center = this.map.getCenter();
center && center.transform(
this.map.getProjectionObject(),
new OpenLayers.Projection("EPSG:4326")
);
var size = this.map.getSize();
this.attribution = OpenLayers.String.format(this.attributionTemplate, {
t: this.mapTypes[this.type],
zoom: this.map.getZoom(),
center: center.lat + "," + center.lon,
mapType: this.type,
mapData: mapData
});
this.map && this.map.events.triggerEvent("changelayer", {
layer: this,
property: "attribution"
});
},
/**
* Method: setMap
*/
setMap: function() {
OpenLayers.Layer.XYZ.prototype.setMap.apply(this, arguments);
this.events.register("moveend", this,
OpenLayers.Layer.GoogleNG.loadCopyrights
);
},
/**
* Method: removeMap
*/
removeMap: function() {
OpenLayers.Layer.XYZ.prototype.removeMap.apply(this, arguments);
this.events.unregister("moveend", this,
OpenLayers.Layer.GoogleNG.loadCopyrights
);
},
/**
* APIMethod: clone
*
* Parameters:
* obj - {Object}
*
* Returns:
* {<OpenLayers.Layer.GoogleNG>} An exact clone of this
* <OpenLayers.Layer.GoogleNG>
*/
clone: function(obj) {
if (obj == null) {
obj = new OpenLayers.Layer.GoogleNG(this.options);
}
//get all additions from superclasses
obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
return obj;
},
CLASS_NAME: "OpenLayers.Layer.GoogleNG"
});
/**
* Property: mapObject
* {google.maps.Map} Shared GMaps instance - will be set upon instantiation of
* the 1st GoogleNG layer
*/
OpenLayers.Layer.GoogleNG.mapObject = null;
/**
* Function: loadCopyrights
* Using the Google Maps Copyright Service mode (see
* http://mapki.com/wiki/Google_Map_Parameters#Misc) to get the attribution for
* the current map extent. Will be called by each GoogleNG layer instance on
* moveend.
*/
OpenLayers.Layer.GoogleNG.loadCopyrights = function() {
var me = OpenLayers.Layer.GoogleNG.loadCopyrights;
if (me.numLoadingScripts == undefined) {
me.loadingScripts = [];
me.numLoadingScripts = 0;
me.copyrights = {"m": [], "k": [], "h": [], "p": []};
// store window scope functions before overwriting them
me.origGAddCopyright = window.GAddCopyright;
me.origGVerify = window.GVerify;
me.origGAppFeatures = window.GAppFeatures;
// defining window scope functions called by the script that the
// Copyright Service returns
window.GAddCopyright = function() {
var copyright = arguments[7];
var category = me.copyrights[arguments[0]];
if (OpenLayers.Util.indexOf(category, copyright) == -1) {
copyright && category.push(copyright);
}
};
window.GVerify = OpenLayers.Function.True;
window.GAppFeatures = OpenLayers.Function.bind(function() {
me.numLoadingScripts--;
if (me.numLoadingScripts == 0) {
var script;
for (var i=me.loadingScripts.length-1; i>=0; --i) {
script = me.loadingScripts[i][0];
me.loadingScripts[i][1].updateAttribution(me.copyrights);
script.parentNode.removeChild(script);
}
// restore original functions
window.GAddCopyright = me.origGAddCopyright;
delete me.origGAddCopyright;
window.GVerify = me.origGVerify;
delete me.origGVerify;
window.GAppFeatures = me.origGAppFeatures;
delete me.origGAppFeatures;
delete me.loadingScripts;
delete me.numLoadingScripts;
delete me.copyrights;
}
}, this);
}
var mapProj = this.map.getProjectionObject();
var llProj = new OpenLayers.Projection("EPSG:4326");
var center = this.map.getCenter().transform(mapProj, llProj);
var extent = this.map.getExtent().transform(mapProj, llProj);
var params = {
spn: extent.getHeight() + "," + extent.getWidth(),
z: this.map.getZoom(),
t: this.mapTypes[this.type],
vp: center.lat + "," + center.lon
};
var url = "http://maps.google.com/maps?" +
OpenLayers.Util.getParameterString(params);
var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
me.numLoadingScripts++;
me.loadingScripts.push([script, this]);
document.getElementsByTagName("head")[0].appendChild(script);
};