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.js
|
9
|
* @requires OpenLayers/Util.js
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Class: OpenLayers.Layer.EventPane
|
14
|
* Base class for 3rd party layers, providing a DOM element which isolates
|
15
|
* the 3rd-party layer from mouse events.
|
16
|
* Only used by Google layers.
|
17
|
*
|
18
|
* Automatically instantiated by the Google constructor, and not usually instantiated directly.
|
19
|
*
|
20
|
* Create a new event pane layer with the
|
21
|
* <OpenLayers.Layer.EventPane> constructor.
|
22
|
*
|
23
|
* Inherits from:
|
24
|
* - <OpenLayers.Layer>
|
25
|
*/
|
26
|
OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
|
27
|
|
28
|
/**
|
29
|
* APIProperty: smoothDragPan
|
30
|
* {Boolean} smoothDragPan determines whether non-public/internal API
|
31
|
* methods are used for better performance while dragging EventPane
|
32
|
* layers. When not in sphericalMercator mode, the smoother dragging
|
33
|
* doesn't actually move north/south directly with the number of
|
34
|
* pixels moved, resulting in a slight offset when you drag your mouse
|
35
|
* north south with this option on. If this visual disparity bothers
|
36
|
* you, you should turn this option off, or use spherical mercator.
|
37
|
* Default is on.
|
38
|
*/
|
39
|
smoothDragPan: true,
|
40
|
|
41
|
/**
|
42
|
* Property: isBaseLayer
|
43
|
* {Boolean} EventPaned layers are always base layers, by necessity.
|
44
|
*/
|
45
|
isBaseLayer: true,
|
46
|
|
47
|
/**
|
48
|
* APIProperty: isFixed
|
49
|
* {Boolean} EventPaned layers are fixed by default.
|
50
|
*/
|
51
|
isFixed: true,
|
52
|
|
53
|
/**
|
54
|
* Property: pane
|
55
|
* {DOMElement} A reference to the element that controls the events.
|
56
|
*/
|
57
|
pane: null,
|
58
|
|
59
|
|
60
|
/**
|
61
|
* Property: mapObject
|
62
|
* {Object} This is the object which will be used to load the 3rd party library
|
63
|
* in the case of the google layer, this will be of type GMap,
|
64
|
* in the case of the ve layer, this will be of type VEMap
|
65
|
*/
|
66
|
mapObject: null,
|
67
|
|
68
|
|
69
|
/**
|
70
|
* Constructor: OpenLayers.Layer.EventPane
|
71
|
* Create a new event pane layer
|
72
|
*
|
73
|
* Parameters:
|
74
|
* name - {String}
|
75
|
* options - {Object} Hashtable of extra options to tag onto the layer
|
76
|
*/
|
77
|
initialize: function(name, options) {
|
78
|
OpenLayers.Layer.prototype.initialize.apply(this, arguments);
|
79
|
if (this.pane == null) {
|
80
|
this.pane = OpenLayers.Util.createDiv(this.div.id + "_EventPane");
|
81
|
}
|
82
|
},
|
83
|
|
84
|
/**
|
85
|
* APIMethod: destroy
|
86
|
* Deconstruct this layer.
|
87
|
*/
|
88
|
destroy: function() {
|
89
|
this.mapObject = null;
|
90
|
this.pane = null;
|
91
|
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
|
92
|
},
|
93
|
|
94
|
|
95
|
/**
|
96
|
* Method: setMap
|
97
|
* Set the map property for the layer. This is done through an accessor
|
98
|
* so that subclasses can override this and take special action once
|
99
|
* they have their map variable set.
|
100
|
*
|
101
|
* Parameters:
|
102
|
* map - {<OpenLayers.Map>}
|
103
|
*/
|
104
|
setMap: function(map) {
|
105
|
OpenLayers.Layer.prototype.setMap.apply(this, arguments);
|
106
|
|
107
|
this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
|
108
|
this.pane.style.display = this.div.style.display;
|
109
|
this.pane.style.width="100%";
|
110
|
this.pane.style.height="100%";
|
111
|
if (OpenLayers.BROWSER_NAME == "msie") {
|
112
|
this.pane.style.background =
|
113
|
"url(" + OpenLayers.Util.getImageLocation("blank.gif") + ")";
|
114
|
}
|
115
|
|
116
|
if (this.isFixed) {
|
117
|
this.map.viewPortDiv.appendChild(this.pane);
|
118
|
} else {
|
119
|
this.map.layerContainerDiv.appendChild(this.pane);
|
120
|
}
|
121
|
|
122
|
// once our layer has been added to the map, we can load it
|
123
|
this.loadMapObject();
|
124
|
|
125
|
// if map didn't load, display warning
|
126
|
if (this.mapObject == null) {
|
127
|
this.loadWarningMessage();
|
128
|
}
|
129
|
},
|
130
|
|
131
|
/**
|
132
|
* APIMethod: removeMap
|
133
|
* On being removed from the map, we'll like to remove the invisible 'pane'
|
134
|
* div that we added to it on creation.
|
135
|
*
|
136
|
* Parameters:
|
137
|
* map - {<OpenLayers.Map>}
|
138
|
*/
|
139
|
removeMap: function(map) {
|
140
|
if (this.pane && this.pane.parentNode) {
|
141
|
this.pane.parentNode.removeChild(this.pane);
|
142
|
}
|
143
|
OpenLayers.Layer.prototype.removeMap.apply(this, arguments);
|
144
|
},
|
145
|
|
146
|
/**
|
147
|
* Method: loadWarningMessage
|
148
|
* If we can't load the map lib, then display an error message to the
|
149
|
* user and tell them where to go for help.
|
150
|
*
|
151
|
* This function sets up the layout for the warning message. Each 3rd
|
152
|
* party layer must implement its own getWarningHTML() function to
|
153
|
* provide the actual warning message.
|
154
|
*/
|
155
|
loadWarningMessage:function() {
|
156
|
|
157
|
this.div.style.backgroundColor = "darkblue";
|
158
|
|
159
|
var viewSize = this.map.getSize();
|
160
|
|
161
|
var msgW = Math.min(viewSize.w, 300);
|
162
|
var msgH = Math.min(viewSize.h, 200);
|
163
|
var size = new OpenLayers.Size(msgW, msgH);
|
164
|
|
165
|
var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2);
|
166
|
|
167
|
var topLeft = centerPx.add(-size.w/2, -size.h/2);
|
168
|
|
169
|
var div = OpenLayers.Util.createDiv(this.name + "_warning",
|
170
|
topLeft,
|
171
|
size,
|
172
|
null,
|
173
|
null,
|
174
|
null,
|
175
|
"auto");
|
176
|
|
177
|
div.style.padding = "7px";
|
178
|
div.style.backgroundColor = "yellow";
|
179
|
|
180
|
div.innerHTML = this.getWarningHTML();
|
181
|
this.div.appendChild(div);
|
182
|
},
|
183
|
|
184
|
/**
|
185
|
* Method: getWarningHTML
|
186
|
* To be implemented by subclasses.
|
187
|
*
|
188
|
* Returns:
|
189
|
* {String} String with information on why layer is broken, how to get
|
190
|
* it working.
|
191
|
*/
|
192
|
getWarningHTML:function() {
|
193
|
//should be implemented by subclasses
|
194
|
return "";
|
195
|
},
|
196
|
|
197
|
/**
|
198
|
* Method: display
|
199
|
* Set the display on the pane
|
200
|
*
|
201
|
* Parameters:
|
202
|
* display - {Boolean}
|
203
|
*/
|
204
|
display: function(display) {
|
205
|
OpenLayers.Layer.prototype.display.apply(this, arguments);
|
206
|
this.pane.style.display = this.div.style.display;
|
207
|
},
|
208
|
|
209
|
/**
|
210
|
* Method: setZIndex
|
211
|
* Set the z-index order for the pane.
|
212
|
*
|
213
|
* Parameters:
|
214
|
* zIndex - {int}
|
215
|
*/
|
216
|
setZIndex: function (zIndex) {
|
217
|
OpenLayers.Layer.prototype.setZIndex.apply(this, arguments);
|
218
|
this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
|
219
|
},
|
220
|
|
221
|
/**
|
222
|
* Method: moveByPx
|
223
|
* Move the layer based on pixel vector. To be implemented by subclasses.
|
224
|
*
|
225
|
* Parameters:
|
226
|
* dx - {Number} The x coord of the displacement vector.
|
227
|
* dy - {Number} The y coord of the displacement vector.
|
228
|
*/
|
229
|
moveByPx: function(dx, dy) {
|
230
|
OpenLayers.Layer.prototype.moveByPx.apply(this, arguments);
|
231
|
|
232
|
if (this.dragPanMapObject) {
|
233
|
this.dragPanMapObject(dx, -dy);
|
234
|
} else {
|
235
|
this.moveTo(this.map.getCachedCenter());
|
236
|
}
|
237
|
},
|
238
|
|
239
|
/**
|
240
|
* Method: moveTo
|
241
|
* Handle calls to move the layer.
|
242
|
*
|
243
|
* Parameters:
|
244
|
* bounds - {<OpenLayers.Bounds>}
|
245
|
* zoomChanged - {Boolean}
|
246
|
* dragging - {Boolean}
|
247
|
*/
|
248
|
moveTo:function(bounds, zoomChanged, dragging) {
|
249
|
OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
|
250
|
|
251
|
if (this.mapObject != null) {
|
252
|
|
253
|
var newCenter = this.map.getCenter();
|
254
|
var newZoom = this.map.getZoom();
|
255
|
|
256
|
if (newCenter != null) {
|
257
|
|
258
|
var moOldCenter = this.getMapObjectCenter();
|
259
|
var oldCenter = this.getOLLonLatFromMapObjectLonLat(moOldCenter);
|
260
|
|
261
|
var moOldZoom = this.getMapObjectZoom();
|
262
|
var oldZoom= this.getOLZoomFromMapObjectZoom(moOldZoom);
|
263
|
|
264
|
if (!(newCenter.equals(oldCenter)) || newZoom != oldZoom) {
|
265
|
|
266
|
if (!zoomChanged && oldCenter && this.dragPanMapObject &&
|
267
|
this.smoothDragPan) {
|
268
|
var oldPx = this.map.getViewPortPxFromLonLat(oldCenter);
|
269
|
var newPx = this.map.getViewPortPxFromLonLat(newCenter);
|
270
|
this.dragPanMapObject(newPx.x-oldPx.x, oldPx.y-newPx.y);
|
271
|
} else {
|
272
|
var center = this.getMapObjectLonLatFromOLLonLat(newCenter);
|
273
|
var zoom = this.getMapObjectZoomFromOLZoom(newZoom);
|
274
|
this.setMapObjectCenter(center, zoom, dragging);
|
275
|
}
|
276
|
}
|
277
|
}
|
278
|
}
|
279
|
},
|
280
|
|
281
|
|
282
|
/********************************************************/
|
283
|
/* */
|
284
|
/* Baselayer Functions */
|
285
|
/* */
|
286
|
/********************************************************/
|
287
|
|
288
|
/**
|
289
|
* Method: getLonLatFromViewPortPx
|
290
|
* Get a map location from a pixel location
|
291
|
*
|
292
|
* Parameters:
|
293
|
* viewPortPx - {<OpenLayers.Pixel>}
|
294
|
*
|
295
|
* Returns:
|
296
|
* {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view
|
297
|
* port OpenLayers.Pixel, translated into lon/lat by map lib
|
298
|
* If the map lib is not loaded or not centered, returns null
|
299
|
*/
|
300
|
getLonLatFromViewPortPx: function (viewPortPx) {
|
301
|
var lonlat = null;
|
302
|
if ( (this.mapObject != null) &&
|
303
|
(this.getMapObjectCenter() != null) ) {
|
304
|
var moPixel = this.getMapObjectPixelFromOLPixel(viewPortPx);
|
305
|
var moLonLat = this.getMapObjectLonLatFromMapObjectPixel(moPixel);
|
306
|
lonlat = this.getOLLonLatFromMapObjectLonLat(moLonLat);
|
307
|
}
|
308
|
return lonlat;
|
309
|
},
|
310
|
|
311
|
|
312
|
/**
|
313
|
* Method: getViewPortPxFromLonLat
|
314
|
* Get a pixel location from a map location
|
315
|
*
|
316
|
* Parameters:
|
317
|
* lonlat - {<OpenLayers.LonLat>}
|
318
|
*
|
319
|
* Returns:
|
320
|
* {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in
|
321
|
* OpenLayers.LonLat, translated into view port pixels by map lib
|
322
|
* If map lib is not loaded or not centered, returns null
|
323
|
*/
|
324
|
getViewPortPxFromLonLat: function (lonlat) {
|
325
|
var viewPortPx = null;
|
326
|
if ( (this.mapObject != null) &&
|
327
|
(this.getMapObjectCenter() != null) ) {
|
328
|
|
329
|
var moLonLat = this.getMapObjectLonLatFromOLLonLat(lonlat);
|
330
|
var moPixel = this.getMapObjectPixelFromMapObjectLonLat(moLonLat);
|
331
|
|
332
|
viewPortPx = this.getOLPixelFromMapObjectPixel(moPixel);
|
333
|
}
|
334
|
return viewPortPx;
|
335
|
},
|
336
|
|
337
|
/********************************************************/
|
338
|
/* */
|
339
|
/* Translation Functions */
|
340
|
/* */
|
341
|
/* The following functions translate Map Object and */
|
342
|
/* OL formats for Pixel, LonLat */
|
343
|
/* */
|
344
|
/********************************************************/
|
345
|
|
346
|
//
|
347
|
// TRANSLATION: MapObject LatLng <-> OpenLayers.LonLat
|
348
|
//
|
349
|
|
350
|
/**
|
351
|
* Method: getOLLonLatFromMapObjectLonLat
|
352
|
* Get an OL style map location from a 3rd party style map location
|
353
|
*
|
354
|
* Parameters
|
355
|
* moLonLat - {Object}
|
356
|
*
|
357
|
* Returns:
|
358
|
* {<OpenLayers.LonLat>} An OpenLayers.LonLat, translated from the passed in
|
359
|
* MapObject LonLat
|
360
|
* Returns null if null value is passed in
|
361
|
*/
|
362
|
getOLLonLatFromMapObjectLonLat: function(moLonLat) {
|
363
|
var olLonLat = null;
|
364
|
if (moLonLat != null) {
|
365
|
var lon = this.getLongitudeFromMapObjectLonLat(moLonLat);
|
366
|
var lat = this.getLatitudeFromMapObjectLonLat(moLonLat);
|
367
|
olLonLat = new OpenLayers.LonLat(lon, lat);
|
368
|
}
|
369
|
return olLonLat;
|
370
|
},
|
371
|
|
372
|
/**
|
373
|
* Method: getMapObjectLonLatFromOLLonLat
|
374
|
* Get a 3rd party map location from an OL map location.
|
375
|
*
|
376
|
* Parameters:
|
377
|
* olLonLat - {<OpenLayers.LonLat>}
|
378
|
*
|
379
|
* Returns:
|
380
|
* {Object} A MapObject LonLat, translated from the passed in
|
381
|
* OpenLayers.LonLat
|
382
|
* Returns null if null value is passed in
|
383
|
*/
|
384
|
getMapObjectLonLatFromOLLonLat: function(olLonLat) {
|
385
|
var moLatLng = null;
|
386
|
if (olLonLat != null) {
|
387
|
moLatLng = this.getMapObjectLonLatFromLonLat(olLonLat.lon,
|
388
|
olLonLat.lat);
|
389
|
}
|
390
|
return moLatLng;
|
391
|
},
|
392
|
|
393
|
|
394
|
//
|
395
|
// TRANSLATION: MapObject Pixel <-> OpenLayers.Pixel
|
396
|
//
|
397
|
|
398
|
/**
|
399
|
* Method: getOLPixelFromMapObjectPixel
|
400
|
* Get an OL pixel location from a 3rd party pixel location.
|
401
|
*
|
402
|
* Parameters:
|
403
|
* moPixel - {Object}
|
404
|
*
|
405
|
* Returns:
|
406
|
* {<OpenLayers.Pixel>} An OpenLayers.Pixel, translated from the passed in
|
407
|
* MapObject Pixel
|
408
|
* Returns null if null value is passed in
|
409
|
*/
|
410
|
getOLPixelFromMapObjectPixel: function(moPixel) {
|
411
|
var olPixel = null;
|
412
|
if (moPixel != null) {
|
413
|
var x = this.getXFromMapObjectPixel(moPixel);
|
414
|
var y = this.getYFromMapObjectPixel(moPixel);
|
415
|
olPixel = new OpenLayers.Pixel(x, y);
|
416
|
}
|
417
|
return olPixel;
|
418
|
},
|
419
|
|
420
|
/**
|
421
|
* Method: getMapObjectPixelFromOLPixel
|
422
|
* Get a 3rd party pixel location from an OL pixel location
|
423
|
*
|
424
|
* Parameters:
|
425
|
* olPixel - {<OpenLayers.Pixel>}
|
426
|
*
|
427
|
* Returns:
|
428
|
* {Object} A MapObject Pixel, translated from the passed in
|
429
|
* OpenLayers.Pixel
|
430
|
* Returns null if null value is passed in
|
431
|
*/
|
432
|
getMapObjectPixelFromOLPixel: function(olPixel) {
|
433
|
var moPixel = null;
|
434
|
if (olPixel != null) {
|
435
|
moPixel = this.getMapObjectPixelFromXY(olPixel.x, olPixel.y);
|
436
|
}
|
437
|
return moPixel;
|
438
|
},
|
439
|
|
440
|
CLASS_NAME: "OpenLayers.Layer.EventPane"
|
441
|
});
|