Boxes.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (c) 2006-2011 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. * @requires OpenLayers/Layer.js
  7. * @requires OpenLayers/Layer/Markers.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.Boxes
  11. * Draw divs as 'boxes' on the layer.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Layer.Markers>
  15. */
  16. OpenLayers.Layer.Boxes = OpenLayers.Class(OpenLayers.Layer.Markers, {
  17. /**
  18. * Constructor: OpenLayers.Layer.Boxes
  19. *
  20. * Parameters:
  21. * name - {String}
  22. * options - {Object} Hashtable of extra options to tag onto the layer
  23. */
  24. initialize: function (name, options) {
  25. OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
  26. },
  27. /**
  28. * Method: drawMarker
  29. * Calculate the pixel location for the marker, create it, and
  30. * add it to the layer's div
  31. *
  32. * Parameters:
  33. * marker - {<OpenLayers.Marker.Box>}
  34. */
  35. drawMarker: function(marker) {
  36. var bounds = marker.bounds;
  37. var topleft = this.map.getLayerPxFromLonLat(
  38. new OpenLayers.LonLat(bounds.left, bounds.top));
  39. var botright = this.map.getLayerPxFromLonLat(
  40. new OpenLayers.LonLat(bounds.right, bounds.bottom));
  41. if (botright == null || topleft == null) {
  42. marker.display(false);
  43. } else {
  44. var sz = new OpenLayers.Size(
  45. Math.max(1, botright.x - topleft.x),
  46. Math.max(1, botright.y - topleft.y));
  47. var markerDiv = marker.draw(topleft, sz);
  48. if (!marker.drawn) {
  49. this.div.appendChild(markerDiv);
  50. marker.drawn = true;
  51. }
  52. }
  53. },
  54. /**
  55. * APIMethod: removeMarker
  56. *
  57. * Parameters:
  58. * marker - {<OpenLayers.Marker.Box>}
  59. */
  60. removeMarker: function(marker) {
  61. OpenLayers.Util.removeItem(this.markers, marker);
  62. if ((marker.div != null) &&
  63. (marker.div.parentNode == this.div) ) {
  64. this.div.removeChild(marker.div);
  65. }
  66. },
  67. CLASS_NAME: "OpenLayers.Layer.Boxes"
  68. });