Feature.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/BaseTypes/Class.js
  7. * @requires OpenLayers/Util.js
  8. */
  9. /**
  10. * Class: OpenLayers.Feature
  11. * Features are combinations of geography and attributes. The OpenLayers.Feature
  12. * class specifically combines a marker and a lonlat.
  13. */
  14. OpenLayers.Feature = OpenLayers.Class({
  15. /**
  16. * Property: layer
  17. * {<OpenLayers.Layer>}
  18. */
  19. layer: null,
  20. /**
  21. * Property: id
  22. * {String}
  23. */
  24. id: null,
  25. /**
  26. * Property: lonlat
  27. * {<OpenLayers.LonLat>}
  28. */
  29. lonlat: null,
  30. /**
  31. * Property: data
  32. * {Object}
  33. */
  34. data: null,
  35. /**
  36. * Property: marker
  37. * {<OpenLayers.Marker>}
  38. */
  39. marker: null,
  40. /**
  41. * APIProperty: popupClass
  42. * {<OpenLayers.Class>} The class which will be used to instantiate
  43. * a new Popup. Default is <OpenLayers.Popup.AnchoredBubble>.
  44. */
  45. popupClass: null,
  46. /**
  47. * Property: popup
  48. * {<OpenLayers.Popup>}
  49. */
  50. popup: null,
  51. /**
  52. * Constructor: OpenLayers.Feature
  53. * Constructor for features.
  54. *
  55. * Parameters:
  56. * layer - {<OpenLayers.Layer>}
  57. * lonlat - {<OpenLayers.LonLat>}
  58. * data - {Object}
  59. *
  60. * Returns:
  61. * {<OpenLayers.Feature>}
  62. */
  63. initialize: function(layer, lonlat, data) {
  64. this.layer = layer;
  65. this.lonlat = lonlat;
  66. this.data = (data != null) ? data : {};
  67. this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
  68. },
  69. /**
  70. * Method: destroy
  71. * nullify references to prevent circular references and memory leaks
  72. */
  73. destroy: function() {
  74. //remove the popup from the map
  75. if ((this.layer != null) && (this.layer.map != null)) {
  76. if (this.popup != null) {
  77. this.layer.map.removePopup(this.popup);
  78. }
  79. }
  80. // remove the marker from the layer
  81. if (this.layer != null && this.marker != null) {
  82. this.layer.removeMarker(this.marker);
  83. }
  84. this.layer = null;
  85. this.id = null;
  86. this.lonlat = null;
  87. this.data = null;
  88. if (this.marker != null) {
  89. this.destroyMarker(this.marker);
  90. this.marker = null;
  91. }
  92. if (this.popup != null) {
  93. this.destroyPopup(this.popup);
  94. this.popup = null;
  95. }
  96. },
  97. /**
  98. * Method: onScreen
  99. *
  100. * Returns:
  101. * {Boolean} Whether or not the feature is currently visible on screen
  102. * (based on its 'lonlat' property)
  103. */
  104. onScreen:function() {
  105. var onScreen = false;
  106. if ((this.layer != null) && (this.layer.map != null)) {
  107. var screenBounds = this.layer.map.getExtent();
  108. onScreen = screenBounds.containsLonLat(this.lonlat);
  109. }
  110. return onScreen;
  111. },
  112. /**
  113. * Method: createMarker
  114. * Based on the data associated with the Feature, create and return a marker object.
  115. *
  116. * Returns:
  117. * {<OpenLayers.Marker>} A Marker Object created from the 'lonlat' and 'icon' properties
  118. * set in this.data. If no 'lonlat' is set, returns null. If no
  119. * 'icon' is set, OpenLayers.Marker() will load the default image.
  120. *
  121. * Note - this.marker is set to return value
  122. *
  123. */
  124. createMarker: function() {
  125. if (this.lonlat != null) {
  126. this.marker = new OpenLayers.Marker(this.lonlat, this.data.icon);
  127. }
  128. return this.marker;
  129. },
  130. /**
  131. * Method: destroyMarker
  132. * Destroys marker.
  133. * If user overrides the createMarker() function, s/he should be able
  134. * to also specify an alternative function for destroying it
  135. */
  136. destroyMarker: function() {
  137. this.marker.destroy();
  138. },
  139. /**
  140. * Method: createPopup
  141. * Creates a popup object created from the 'lonlat', 'popupSize',
  142. * and 'popupContentHTML' properties set in this.data. It uses
  143. * this.marker.icon as default anchor.
  144. *
  145. * If no 'lonlat' is set, returns null.
  146. * If no this.marker has been created, no anchor is sent.
  147. *
  148. * Note - the returned popup object is 'owned' by the feature, so you
  149. * cannot use the popup's destroy method to discard the popup.
  150. * Instead, you must use the feature's destroyPopup
  151. *
  152. * Note - this.popup is set to return value
  153. *
  154. * Parameters:
  155. * closeBox - {Boolean} create popup with closebox or not
  156. *
  157. * Returns:
  158. * {<OpenLayers.Popup>} Returns the created popup, which is also set
  159. * as 'popup' property of this feature. Will be of whatever type
  160. * specified by this feature's 'popupClass' property, but must be
  161. * of type <OpenLayers.Popup>.
  162. *
  163. */
  164. createPopup: function(closeBox) {
  165. if (this.lonlat != null) {
  166. if (!this.popup) {
  167. var anchor = (this.marker) ? this.marker.icon : null;
  168. var popupClass = this.popupClass ?
  169. this.popupClass : OpenLayers.Popup.AnchoredBubble;
  170. this.popup = new popupClass(this.id + "_popup",
  171. this.lonlat,
  172. this.data.popupSize,
  173. this.data.popupContentHTML,
  174. anchor,
  175. closeBox);
  176. }
  177. if (this.data.overflow != null) {
  178. this.popup.contentDiv.style.overflow = this.data.overflow;
  179. }
  180. this.popup.feature = this;
  181. }
  182. return this.popup;
  183. },
  184. /**
  185. * Method: destroyPopup
  186. * Destroys the popup created via createPopup.
  187. *
  188. * As with the marker, if user overrides the createPopup() function, s/he
  189. * should also be able to override the destruction
  190. */
  191. destroyPopup: function() {
  192. if (this.popup) {
  193. this.popup.feature = null;
  194. this.popup.destroy();
  195. this.popup = null;
  196. }
  197. },
  198. CLASS_NAME: "OpenLayers.Feature"
  199. });