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