1
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
|
2
|
* full list of contributors). Published under the 2-clause BSD license.
|
3
|
* See license.txt in the OpenLayers distribution or repository for the
|
4
|
* full text of the license. */
|
5
|
|
6
|
|
7
|
/**
|
8
|
* @requires OpenLayers/Layer/Google.js
|
9
|
*/
|
10
|
|
11
|
/**
|
12
|
* Constant: OpenLayers.Layer.Google.v3
|
13
|
*
|
14
|
* Mixin providing functionality specific to the Google Maps API v3.
|
15
|
*
|
16
|
* To use this layer, you must include the GMaps v3 API in your html.
|
17
|
*
|
18
|
* Note that this layer configures the google.maps.map object with the
|
19
|
* "disableDefaultUI" option set to true. Using UI controls that the Google
|
20
|
* Maps API provides is not supported by the OpenLayers API.
|
21
|
*/
|
22
|
OpenLayers.Layer.Google.v3 = {
|
23
|
|
24
|
/**
|
25
|
* Constant: DEFAULTS
|
26
|
* {Object} It is not recommended to change the properties set here. Note
|
27
|
* that Google.v3 layers only work when sphericalMercator is set to true.
|
28
|
*
|
29
|
* (code)
|
30
|
* {
|
31
|
* sphericalMercator: true,
|
32
|
* projection: "EPSG:900913"
|
33
|
* }
|
34
|
* (end)
|
35
|
*/
|
36
|
DEFAULTS: {
|
37
|
sphericalMercator: true,
|
38
|
projection: "EPSG:900913"
|
39
|
},
|
40
|
|
41
|
/**
|
42
|
* APIProperty: animationEnabled
|
43
|
* {Boolean} If set to true, the transition between zoom levels will be
|
44
|
* animated (if supported by the GMaps API for the device used). Set to
|
45
|
* false to match the zooming experience of other layer types. Default
|
46
|
* is true. Note that the GMaps API does not give us control over zoom
|
47
|
* animation, so if set to false, when zooming, this will make the
|
48
|
* layer temporarily invisible, wait until GMaps reports the map being
|
49
|
* idle, and make it visible again. The result will be a blank layer
|
50
|
* for a few moments while zooming.
|
51
|
*/
|
52
|
animationEnabled: true,
|
53
|
|
54
|
/**
|
55
|
* Method: loadMapObject
|
56
|
* Load the GMap and register appropriate event listeners.
|
57
|
*/
|
58
|
loadMapObject: function() {
|
59
|
if (!this.type) {
|
60
|
this.type = google.maps.MapTypeId.ROADMAP;
|
61
|
}
|
62
|
var mapObject;
|
63
|
var cache = OpenLayers.Layer.Google.cache[this.map.id];
|
64
|
if (cache) {
|
65
|
// there are already Google layers added to this map
|
66
|
mapObject = cache.mapObject;
|
67
|
// increment the layer count
|
68
|
++cache.count;
|
69
|
} else {
|
70
|
// this is the first Google layer for this map
|
71
|
// create GMap
|
72
|
var center = this.map.getCenter();
|
73
|
var container = document.createElement('div');
|
74
|
container.className = "olForeignContainer";
|
75
|
container.style.width = '100%';
|
76
|
container.style.height = '100%';
|
77
|
mapObject = new google.maps.Map(container, {
|
78
|
center: center ?
|
79
|
new google.maps.LatLng(center.lat, center.lon) :
|
80
|
new google.maps.LatLng(0, 0),
|
81
|
zoom: this.map.getZoom() || 0,
|
82
|
mapTypeId: this.type,
|
83
|
disableDefaultUI: true,
|
84
|
keyboardShortcuts: false,
|
85
|
draggable: false,
|
86
|
disableDoubleClickZoom: true,
|
87
|
scrollwheel: false,
|
88
|
streetViewControl: false
|
89
|
});
|
90
|
var googleControl = document.createElement('div');
|
91
|
googleControl.style.width = '100%';
|
92
|
googleControl.style.height = '100%';
|
93
|
mapObject.controls[google.maps.ControlPosition.TOP_LEFT].push(googleControl);
|
94
|
|
95
|
// cache elements for use by any other google layers added to
|
96
|
// this same map
|
97
|
cache = {
|
98
|
googleControl: googleControl,
|
99
|
mapObject: mapObject,
|
100
|
count: 1
|
101
|
};
|
102
|
OpenLayers.Layer.Google.cache[this.map.id] = cache;
|
103
|
}
|
104
|
this.mapObject = mapObject;
|
105
|
this.setGMapVisibility(this.visibility);
|
106
|
},
|
107
|
|
108
|
/**
|
109
|
* APIMethod: onMapResize
|
110
|
*/
|
111
|
onMapResize: function() {
|
112
|
if (this.visibility) {
|
113
|
google.maps.event.trigger(this.mapObject, "resize");
|
114
|
}
|
115
|
},
|
116
|
|
117
|
/**
|
118
|
* Method: setGMapVisibility
|
119
|
* Display the GMap container and associated elements.
|
120
|
*
|
121
|
* Parameters:
|
122
|
* visible - {Boolean} Display the GMap elements.
|
123
|
*/
|
124
|
setGMapVisibility: function(visible) {
|
125
|
var cache = OpenLayers.Layer.Google.cache[this.map.id];
|
126
|
var map = this.map;
|
127
|
if (cache) {
|
128
|
var type = this.type;
|
129
|
var layers = map.layers;
|
130
|
var layer;
|
131
|
for (var i=layers.length-1; i>=0; --i) {
|
132
|
layer = layers[i];
|
133
|
if (layer instanceof OpenLayers.Layer.Google &&
|
134
|
layer.visibility === true && layer.inRange === true) {
|
135
|
type = layer.type;
|
136
|
visible = true;
|
137
|
break;
|
138
|
}
|
139
|
}
|
140
|
var container = this.mapObject.getDiv();
|
141
|
if (visible === true) {
|
142
|
if (container.parentNode !== map.div) {
|
143
|
if (!cache.rendered) {
|
144
|
var me = this;
|
145
|
google.maps.event.addListenerOnce(this.mapObject, 'tilesloaded', function() {
|
146
|
cache.rendered = true;
|
147
|
me.setGMapVisibility(me.getVisibility());
|
148
|
me.moveTo(me.map.getCenter());
|
149
|
});
|
150
|
} else {
|
151
|
map.div.appendChild(container);
|
152
|
cache.googleControl.appendChild(map.viewPortDiv);
|
153
|
google.maps.event.trigger(this.mapObject, 'resize');
|
154
|
}
|
155
|
}
|
156
|
this.mapObject.setMapTypeId(type);
|
157
|
} else if (cache.googleControl.hasChildNodes()) {
|
158
|
map.div.appendChild(map.viewPortDiv);
|
159
|
map.div.removeChild(container);
|
160
|
}
|
161
|
}
|
162
|
},
|
163
|
|
164
|
/**
|
165
|
* Method: getMapContainer
|
166
|
*
|
167
|
* Returns:
|
168
|
* {DOMElement} the GMap container's div
|
169
|
*/
|
170
|
getMapContainer: function() {
|
171
|
return this.mapObject.getDiv();
|
172
|
},
|
173
|
|
174
|
//
|
175
|
// TRANSLATION: MapObject Bounds <-> OpenLayers.Bounds
|
176
|
//
|
177
|
|
178
|
/**
|
179
|
* APIMethod: getMapObjectBoundsFromOLBounds
|
180
|
*
|
181
|
* Parameters:
|
182
|
* olBounds - {<OpenLayers.Bounds>}
|
183
|
*
|
184
|
* Returns:
|
185
|
* {Object} A MapObject Bounds, translated from olBounds
|
186
|
* Returns null if null value is passed in
|
187
|
*/
|
188
|
getMapObjectBoundsFromOLBounds: function(olBounds) {
|
189
|
var moBounds = null;
|
190
|
if (olBounds != null) {
|
191
|
var sw = this.sphericalMercator ?
|
192
|
this.inverseMercator(olBounds.bottom, olBounds.left) :
|
193
|
new OpenLayers.LonLat(olBounds.bottom, olBounds.left);
|
194
|
var ne = this.sphericalMercator ?
|
195
|
this.inverseMercator(olBounds.top, olBounds.right) :
|
196
|
new OpenLayers.LonLat(olBounds.top, olBounds.right);
|
197
|
moBounds = new google.maps.LatLngBounds(
|
198
|
new google.maps.LatLng(sw.lat, sw.lon),
|
199
|
new google.maps.LatLng(ne.lat, ne.lon)
|
200
|
);
|
201
|
}
|
202
|
return moBounds;
|
203
|
},
|
204
|
|
205
|
|
206
|
/************************************
|
207
|
* *
|
208
|
* MapObject Interface Controls *
|
209
|
* *
|
210
|
************************************/
|
211
|
|
212
|
|
213
|
// LonLat - Pixel Translation
|
214
|
|
215
|
/**
|
216
|
* APIMethod: getMapObjectLonLatFromMapObjectPixel
|
217
|
*
|
218
|
* Parameters:
|
219
|
* moPixel - {Object} MapObject Pixel format
|
220
|
*
|
221
|
* Returns:
|
222
|
* {Object} MapObject LonLat translated from MapObject Pixel
|
223
|
*/
|
224
|
getMapObjectLonLatFromMapObjectPixel: function(moPixel) {
|
225
|
var size = this.map.getSize();
|
226
|
var lon = this.getLongitudeFromMapObjectLonLat(this.mapObject.center);
|
227
|
var lat = this.getLatitudeFromMapObjectLonLat(this.mapObject.center);
|
228
|
var res = this.map.getResolution();
|
229
|
|
230
|
var delta_x = moPixel.x - (size.w / 2);
|
231
|
var delta_y = moPixel.y - (size.h / 2);
|
232
|
|
233
|
var lonlat = new OpenLayers.LonLat(
|
234
|
lon + delta_x * res,
|
235
|
lat - delta_y * res
|
236
|
);
|
237
|
|
238
|
if (this.wrapDateLine) {
|
239
|
lonlat = lonlat.wrapDateLine(this.maxExtent);
|
240
|
}
|
241
|
return this.getMapObjectLonLatFromLonLat(lonlat.lon, lonlat.lat);
|
242
|
},
|
243
|
|
244
|
/**
|
245
|
* APIMethod: getMapObjectPixelFromMapObjectLonLat
|
246
|
*
|
247
|
* Parameters:
|
248
|
* moLonLat - {Object} MapObject LonLat format
|
249
|
*
|
250
|
* Returns:
|
251
|
* {Object} MapObject Pixel transtlated from MapObject LonLat
|
252
|
*/
|
253
|
getMapObjectPixelFromMapObjectLonLat: function(moLonLat) {
|
254
|
var lon = this.getLongitudeFromMapObjectLonLat(moLonLat);
|
255
|
var lat = this.getLatitudeFromMapObjectLonLat(moLonLat);
|
256
|
var res = this.map.getResolution();
|
257
|
var extent = this.map.getExtent();
|
258
|
return this.getMapObjectPixelFromXY((1/res * (lon - extent.left)),
|
259
|
(1/res * (extent.top - lat)));
|
260
|
},
|
261
|
|
262
|
|
263
|
/**
|
264
|
* APIMethod: setMapObjectCenter
|
265
|
* Set the mapObject to the specified center and zoom
|
266
|
*
|
267
|
* Parameters:
|
268
|
* center - {Object} MapObject LonLat format
|
269
|
* zoom - {int} MapObject zoom format
|
270
|
*/
|
271
|
setMapObjectCenter: function(center, zoom) {
|
272
|
if (this.animationEnabled === false && zoom != this.mapObject.zoom) {
|
273
|
var mapContainer = this.getMapContainer();
|
274
|
google.maps.event.addListenerOnce(
|
275
|
this.mapObject,
|
276
|
"idle",
|
277
|
function() {
|
278
|
mapContainer.style.visibility = "";
|
279
|
}
|
280
|
);
|
281
|
mapContainer.style.visibility = "hidden";
|
282
|
}
|
283
|
this.mapObject.setOptions({
|
284
|
center: center,
|
285
|
zoom: zoom
|
286
|
});
|
287
|
},
|
288
|
|
289
|
|
290
|
// Bounds
|
291
|
|
292
|
/**
|
293
|
* APIMethod: getMapObjectZoomFromMapObjectBounds
|
294
|
*
|
295
|
* Parameters:
|
296
|
* moBounds - {Object} MapObject Bounds format
|
297
|
*
|
298
|
* Returns:
|
299
|
* {Object} MapObject Zoom for specified MapObject Bounds
|
300
|
*/
|
301
|
getMapObjectZoomFromMapObjectBounds: function(moBounds) {
|
302
|
return this.mapObject.getBoundsZoomLevel(moBounds);
|
303
|
},
|
304
|
|
305
|
/************************************
|
306
|
* *
|
307
|
* MapObject Primitives *
|
308
|
* *
|
309
|
************************************/
|
310
|
|
311
|
|
312
|
// LonLat
|
313
|
|
314
|
/**
|
315
|
* APIMethod: getMapObjectLonLatFromLonLat
|
316
|
*
|
317
|
* Parameters:
|
318
|
* lon - {Float}
|
319
|
* lat - {Float}
|
320
|
*
|
321
|
* Returns:
|
322
|
* {Object} MapObject LonLat built from lon and lat params
|
323
|
*/
|
324
|
getMapObjectLonLatFromLonLat: function(lon, lat) {
|
325
|
var gLatLng;
|
326
|
if(this.sphericalMercator) {
|
327
|
var lonlat = this.inverseMercator(lon, lat);
|
328
|
gLatLng = new google.maps.LatLng(lonlat.lat, lonlat.lon);
|
329
|
} else {
|
330
|
gLatLng = new google.maps.LatLng(lat, lon);
|
331
|
}
|
332
|
return gLatLng;
|
333
|
},
|
334
|
|
335
|
// Pixel
|
336
|
|
337
|
/**
|
338
|
* APIMethod: getMapObjectPixelFromXY
|
339
|
*
|
340
|
* Parameters:
|
341
|
* x - {Integer}
|
342
|
* y - {Integer}
|
343
|
*
|
344
|
* Returns:
|
345
|
* {Object} MapObject Pixel from x and y parameters
|
346
|
*/
|
347
|
getMapObjectPixelFromXY: function(x, y) {
|
348
|
return new google.maps.Point(x, y);
|
349
|
}
|
350
|
|
351
|
};
|