Markers.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @requires OpenLayers/Layer.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.Markers
  10. *
  11. * Inherits from:
  12. * - <OpenLayers.Layer>
  13. */
  14. OpenLayers.Layer.Markers = OpenLayers.Class(OpenLayers.Layer, {
  15. /**
  16. * APIProperty: isBaseLayer
  17. * {Boolean} Markers layer is never a base layer.
  18. */
  19. isBaseLayer: false,
  20. /**
  21. * APIProperty: markers
  22. * {Array(<OpenLayers.Marker>)} internal marker list
  23. */
  24. markers: null,
  25. /**
  26. * Property: drawn
  27. * {Boolean} internal state of drawing. This is a workaround for the fact
  28. * that the map does not call moveTo with a zoomChanged when the map is
  29. * first starting up. This lets us catch the case where we have *never*
  30. * drawn the layer, and draw it even if the zoom hasn't changed.
  31. */
  32. drawn: false,
  33. /**
  34. * Constructor: OpenLayers.Layer.Markers
  35. * Create a Markers layer.
  36. *
  37. * Parameters:
  38. * name - {String}
  39. * options - {Object} Hashtable of extra options to tag onto the layer
  40. */
  41. initialize: function(name, options) {
  42. OpenLayers.Layer.prototype.initialize.apply(this, arguments);
  43. this.markers = [];
  44. },
  45. /**
  46. * APIMethod: destroy
  47. */
  48. destroy: function() {
  49. this.clearMarkers();
  50. this.markers = null;
  51. OpenLayers.Layer.prototype.destroy.apply(this, arguments);
  52. },
  53. /**
  54. * APIMethod: setOpacity
  55. * Sets the opacity for all the markers.
  56. *
  57. * Parameters:
  58. * opacity - {Float}
  59. */
  60. setOpacity: function(opacity) {
  61. if (opacity != this.opacity) {
  62. this.opacity = opacity;
  63. for (var i=0, len=this.markers.length; i<len; i++) {
  64. this.markers[i].setOpacity(this.opacity);
  65. }
  66. }
  67. },
  68. /**
  69. * Method: moveTo
  70. *
  71. * Parameters:
  72. * bounds - {<OpenLayers.Bounds>}
  73. * zoomChanged - {Boolean}
  74. * dragging - {Boolean}
  75. */
  76. moveTo:function(bounds, zoomChanged, dragging) {
  77. OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
  78. if (zoomChanged || !this.drawn) {
  79. for(var i=0, len=this.markers.length; i<len; i++) {
  80. this.drawMarker(this.markers[i]);
  81. }
  82. this.drawn = true;
  83. }
  84. },
  85. /**
  86. * APIMethod: addMarker
  87. *
  88. * Parameters:
  89. * marker - {<OpenLayers.Marker>}
  90. */
  91. addMarker: function(marker) {
  92. this.markers.push(marker);
  93. if (this.opacity < 1) {
  94. marker.setOpacity(this.opacity);
  95. }
  96. if (this.map && this.map.getExtent()) {
  97. marker.map = this.map;
  98. this.drawMarker(marker);
  99. }
  100. },
  101. /**
  102. * APIMethod: removeMarker
  103. *
  104. * Parameters:
  105. * marker - {<OpenLayers.Marker>}
  106. */
  107. removeMarker: function(marker) {
  108. if (this.markers && this.markers.length) {
  109. OpenLayers.Util.removeItem(this.markers, marker);
  110. marker.erase();
  111. }
  112. },
  113. /**
  114. * Method: clearMarkers
  115. * This method removes all markers from a layer. The markers are not
  116. * destroyed by this function, but are removed from the list of markers.
  117. */
  118. clearMarkers: function() {
  119. if (this.markers != null) {
  120. while(this.markers.length > 0) {
  121. this.removeMarker(this.markers[0]);
  122. }
  123. }
  124. },
  125. /**
  126. * Method: drawMarker
  127. * Calculate the pixel location for the marker, create it, and
  128. * add it to the layer's div
  129. *
  130. * Parameters:
  131. * marker - {<OpenLayers.Marker>}
  132. */
  133. drawMarker: function(marker) {
  134. var px = this.map.getLayerPxFromLonLat(marker.lonlat);
  135. if (px == null) {
  136. marker.display(false);
  137. } else {
  138. if (!marker.isDrawn()) {
  139. var markerImg = marker.draw(px);
  140. this.div.appendChild(markerImg);
  141. } else if(marker.icon) {
  142. marker.icon.moveTo(px);
  143. }
  144. }
  145. },
  146. /**
  147. * APIMethod: getDataExtent
  148. * Calculates the max extent which includes all of the markers.
  149. *
  150. * Returns:
  151. * {<OpenLayers.Bounds>}
  152. */
  153. getDataExtent: function () {
  154. var maxExtent = null;
  155. if ( this.markers && (this.markers.length > 0)) {
  156. var maxExtent = new OpenLayers.Bounds();
  157. for(var i=0, len=this.markers.length; i<len; i++) {
  158. var marker = this.markers[i];
  159. maxExtent.extend(marker.lonlat);
  160. }
  161. }
  162. return maxExtent;
  163. },
  164. CLASS_NAME: "OpenLayers.Layer.Markers"
  165. });