1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Layer.js
8 * @requires OpenLayers/Util.js
12 * Class: OpenLayers.Layer.EventPane
13 * Base class for 3rd party layers. Create a new event pane layer with the
14 * <OpenLayers.Layer.EventPane> constructor.
17 * - <OpenLayers.Layer>
19 OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
22 * APIProperty: smoothDragPan
23 * {Boolean} smoothDragPan determines whether non-public/internal API
24 * methods are used for better performance while dragging EventPane
25 * layers. When not in sphericalMercator mode, the smoother dragging
26 * doesn't actually move north/south directly with the number of
27 * pixels moved, resulting in a slight offset when you drag your mouse
28 * north south with this option on. If this visual disparity bothers
29 * you, you should turn this option off, or use spherical mercator.
35 * Property: isBaseLayer
36 * {Boolean} EventPaned layers are always base layers, by necessity.
41 * APIProperty: isFixed
42 * {Boolean} EventPaned layers are fixed by default.
48 * {DOMElement} A reference to the element that controls the events.
55 * {Object} This is the object which will be used to load the 3rd party library
56 * in the case of the google layer, this will be of type GMap,
57 * in the case of the ve layer, this will be of type VEMap
63 * Constructor: OpenLayers.Layer.EventPane
64 * Create a new event pane layer
68 * options - {Object} Hashtable of extra options to tag onto the layer
70 initialize: function(name, options) {
71 OpenLayers.Layer.prototype.initialize.apply(this, arguments);
72 if (this.pane == null) {
73 this.pane = OpenLayers.Util.createDiv(this.div.id + "_EventPane");
79 * Deconstruct this layer.
82 this.mapObject = null;
83 OpenLayers.Layer.prototype.destroy.apply(this, arguments);
89 * Set the map property for the layer. This is done through an accessor
90 * so that subclasses can override this and take special action once
91 * they have their map variable set.
94 * map - {<OpenLayers.Map>}
96 setMap: function(map) {
97 OpenLayers.Layer.prototype.setMap.apply(this, arguments);
99 this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
100 this.pane.style.display = this.div.style.display;
101 this.pane.style.width="100%";
102 this.pane.style.height="100%";
103 if (OpenLayers.Util.getBrowserName() == "msie") {
104 this.pane.style.background =
105 "url(" + OpenLayers.Util.getImagesLocation() + "blank.gif)";
109 this.map.viewPortDiv.appendChild(this.pane);
111 this.map.layerContainerDiv.appendChild(this.pane);
114 // once our layer has been added to the map, we can load it
115 this.loadMapObject();
117 // if map didn't load, display warning
118 if (this.mapObject == null) {
119 this.loadWarningMessage();
124 * APIMethod: removeMap
125 * On being removed from the map, we'll like to remove the invisible 'pane'
126 * div that we added to it on creation.
129 * map - {<OpenLayers.Map>}
131 removeMap: function(map) {
132 if (this.pane && this.pane.parentNode) {
133 this.pane.parentNode.removeChild(this.pane);
136 OpenLayers.Layer.prototype.removeMap.apply(this, arguments);
140 * Method: loadWarningMessage
141 * If we can't load the map lib, then display an error message to the
142 * user and tell them where to go for help.
144 * This function sets up the layout for the warning message. Each 3rd
145 * party layer must implement its own getWarningHTML() function to
146 * provide the actual warning message.
148 loadWarningMessage:function() {
150 this.div.style.backgroundColor = "darkblue";
152 var viewSize = this.map.getSize();
154 var msgW = Math.min(viewSize.w, 300);
155 var msgH = Math.min(viewSize.h, 200);
156 var size = new OpenLayers.Size(msgW, msgH);
158 var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2);
160 var topLeft = centerPx.add(-size.w/2, -size.h/2);
162 var div = OpenLayers.Util.createDiv(this.name + "_warning",
170 div.style.padding = "7px";
171 div.style.backgroundColor = "yellow";
173 div.innerHTML = this.getWarningHTML();
174 this.div.appendChild(div);
178 * Method: getWarningHTML
179 * To be implemented by subclasses.
182 * {String} String with information on why layer is broken, how to get
185 getWarningHTML:function() {
186 //should be implemented by subclasses
192 * Set the display on the pane
195 * display - {Boolean}
197 display: function(display) {
198 OpenLayers.Layer.prototype.display.apply(this, arguments);
199 this.pane.style.display = this.div.style.display;
204 * Set the z-index order for the pane.
209 setZIndex: function (zIndex) {
210 OpenLayers.Layer.prototype.setZIndex.apply(this, arguments);
211 this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
216 * Handle calls to move the layer.
219 * bounds - {<OpenLayers.Bounds>}
220 * zoomChanged - {Boolean}
221 * dragging - {Boolean}
223 moveTo:function(bounds, zoomChanged, dragging) {
224 OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
226 if (this.mapObject != null) {
228 var newCenter = this.map.getCenter();
229 var newZoom = this.map.getZoom();
231 if (newCenter != null) {
233 var moOldCenter = this.getMapObjectCenter();
234 var oldCenter = this.getOLLonLatFromMapObjectLonLat(moOldCenter);
236 var moOldZoom = this.getMapObjectZoom();
237 var oldZoom= this.getOLZoomFromMapObjectZoom(moOldZoom);
239 if ( !(newCenter.equals(oldCenter)) ||
240 !(newZoom == oldZoom) ) {
242 if (dragging && this.dragPanMapObject &&
243 this.smoothDragPan) {
244 var oldPx = this.map.getViewPortPxFromLonLat(oldCenter);
245 var newPx = this.map.getViewPortPxFromLonLat(newCenter);
246 this.dragPanMapObject(newPx.x-oldPx.x, oldPx.y-newPx.y);
248 var center = this.getMapObjectLonLatFromOLLonLat(newCenter);
249 var zoom = this.getMapObjectZoomFromOLZoom(newZoom);
250 this.setMapObjectCenter(center, zoom, dragging);
258 /********************************************************/
260 /* Baselayer Functions */
262 /********************************************************/
265 * Method: getLonLatFromViewPortPx
266 * Get a map location from a pixel location
269 * viewPortPx - {<OpenLayers.Pixel>}
272 * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view
273 * port OpenLayers.Pixel, translated into lon/lat by map lib
274 * If the map lib is not loaded or not centered, returns null
276 getLonLatFromViewPortPx: function (viewPortPx) {
278 if ( (this.mapObject != null) &&
279 (this.getMapObjectCenter() != null) ) {
280 var moPixel = this.getMapObjectPixelFromOLPixel(viewPortPx);
281 var moLonLat = this.getMapObjectLonLatFromMapObjectPixel(moPixel);
282 lonlat = this.getOLLonLatFromMapObjectLonLat(moLonLat);
289 * Method: getViewPortPxFromLonLat
290 * Get a pixel location from a map location
293 * lonlat - {<OpenLayers.LonLat>}
296 * {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in
297 * OpenLayers.LonLat, translated into view port pixels by map lib
298 * If map lib is not loaded or not centered, returns null
300 getViewPortPxFromLonLat: function (lonlat) {
301 var viewPortPx = null;
302 if ( (this.mapObject != null) &&
303 (this.getMapObjectCenter() != null) ) {
305 var moLonLat = this.getMapObjectLonLatFromOLLonLat(lonlat);
306 var moPixel = this.getMapObjectPixelFromMapObjectLonLat(moLonLat);
308 viewPortPx = this.getOLPixelFromMapObjectPixel(moPixel);
313 /********************************************************/
315 /* Translation Functions */
317 /* The following functions translate Map Object and */
318 /* OL formats for Pixel, LonLat */
320 /********************************************************/
323 // TRANSLATION: MapObject LatLng <-> OpenLayers.LonLat
327 * Method: getOLLonLatFromMapObjectLonLat
328 * Get an OL style map location from a 3rd party style map location
331 * moLonLat - {Object}
334 * {<OpenLayers.LonLat>} An OpenLayers.LonLat, translated from the passed in
336 * Returns null if null value is passed in
338 getOLLonLatFromMapObjectLonLat: function(moLonLat) {
340 if (moLonLat != null) {
341 var lon = this.getLongitudeFromMapObjectLonLat(moLonLat);
342 var lat = this.getLatitudeFromMapObjectLonLat(moLonLat);
343 olLonLat = new OpenLayers.LonLat(lon, lat);
349 * Method: getMapObjectLonLatFromOLLonLat
350 * Get a 3rd party map location from an OL map location.
353 * olLonLat - {<OpenLayers.LonLat>}
356 * {Object} A MapObject LonLat, translated from the passed in
358 * Returns null if null value is passed in
360 getMapObjectLonLatFromOLLonLat: function(olLonLat) {
362 if (olLonLat != null) {
363 moLatLng = this.getMapObjectLonLatFromLonLat(olLonLat.lon,
371 // TRANSLATION: MapObject Pixel <-> OpenLayers.Pixel
375 * Method: getOLPixelFromMapObjectPixel
376 * Get an OL pixel location from a 3rd party pixel location.
382 * {<OpenLayers.Pixel>} An OpenLayers.Pixel, translated from the passed in
384 * Returns null if null value is passed in
386 getOLPixelFromMapObjectPixel: function(moPixel) {
388 if (moPixel != null) {
389 var x = this.getXFromMapObjectPixel(moPixel);
390 var y = this.getYFromMapObjectPixel(moPixel);
391 olPixel = new OpenLayers.Pixel(x, y);
397 * Method: getMapObjectPixelFromOLPixel
398 * Get a 3rd party pixel location from an OL pixel location
401 * olPixel - {<OpenLayers.Pixel>}
404 * {Object} A MapObject Pixel, translated from the passed in
406 * Returns null if null value is passed in
408 getMapObjectPixelFromOLPixel: function(olPixel) {
410 if (olPixel != null) {
411 moPixel = this.getMapObjectPixelFromXY(olPixel.x, olPixel.y);
416 CLASS_NAME: "OpenLayers.Layer.EventPane"