GeoRSS.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/Markers.js
  7. * @requires OpenLayers/Request/XMLHttpRequest.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.GeoRSS
  11. * Add GeoRSS Point features to your map.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Layer.Markers>
  15. * - <OpenLayers.Layer>
  16. */
  17. OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
  18. /**
  19. * Property: location
  20. * {String} store url of text file
  21. */
  22. location: null,
  23. /**
  24. * Property: features
  25. * {Array(<OpenLayers.Feature>)}
  26. */
  27. features: null,
  28. /**
  29. * APIProperty: formatOptions
  30. * {Object} Hash of options which should be passed to the format when it is
  31. * created. Must be passed in the constructor.
  32. */
  33. formatOptions: null,
  34. /**
  35. * Property: selectedFeature
  36. * {<OpenLayers.Feature>}
  37. */
  38. selectedFeature: null,
  39. /**
  40. * APIProperty: icon
  41. * {<OpenLayers.Icon>}. This determines the Icon to be used on the map
  42. * for this GeoRSS layer.
  43. */
  44. icon: null,
  45. /**
  46. * APIProperty: popupSize
  47. * {<OpenLayers.Size>} This determines the size of GeoRSS popups. If
  48. * not provided, defaults to 250px by 120px.
  49. */
  50. popupSize: null,
  51. /**
  52. * APIProperty: useFeedTitle
  53. * {Boolean} Set layer.name to the first <title> element in the feed. Default is true.
  54. */
  55. useFeedTitle: true,
  56. /**
  57. * Constructor: OpenLayers.Layer.GeoRSS
  58. * Create a GeoRSS Layer.
  59. *
  60. * Parameters:
  61. * name - {String}
  62. * location - {String}
  63. * options - {Object}
  64. */
  65. initialize: function(name, location, options) {
  66. OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name, options]);
  67. this.location = location;
  68. this.features = [];
  69. },
  70. /**
  71. * Method: destroy
  72. */
  73. destroy: function() {
  74. // Warning: Layer.Markers.destroy() must be called prior to calling
  75. // clearFeatures() here, otherwise we leak memory. Indeed, if
  76. // Layer.Markers.destroy() is called after clearFeatures(), it won't be
  77. // able to remove the marker image elements from the layer's div since
  78. // the markers will have been destroyed by clearFeatures().
  79. OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
  80. this.clearFeatures();
  81. this.features = null;
  82. },
  83. /**
  84. * Method: loadRSS
  85. * Start the load of the RSS data. Don't do this when we first add the layer,
  86. * since we may not be visible at any point, and it would therefore be a waste.
  87. */
  88. loadRSS: function() {
  89. if (!this.loaded) {
  90. this.events.triggerEvent("loadstart");
  91. OpenLayers.Request.GET({
  92. url: this.location,
  93. success: this.parseData,
  94. scope: this
  95. });
  96. this.loaded = true;
  97. }
  98. },
  99. /**
  100. * Method: moveTo
  101. * If layer is visible and RSS has not been loaded, load RSS.
  102. *
  103. * Parameters:
  104. * bounds - {Object}
  105. * zoomChanged - {Object}
  106. * minor - {Object}
  107. */
  108. moveTo:function(bounds, zoomChanged, minor) {
  109. OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments);
  110. if(this.visibility && !this.loaded){
  111. this.loadRSS();
  112. }
  113. },
  114. /**
  115. * Method: parseData
  116. * Parse the data returned from the Events call.
  117. *
  118. * Parameters:
  119. * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
  120. */
  121. parseData: function(ajaxRequest) {
  122. var doc = ajaxRequest.responseXML;
  123. if (!doc || !doc.documentElement) {
  124. doc = OpenLayers.Format.XML.prototype.read(ajaxRequest.responseText);
  125. }
  126. if (this.useFeedTitle) {
  127. var name = null;
  128. try {
  129. name = doc.getElementsByTagNameNS('*', 'title')[0].firstChild.nodeValue;
  130. }
  131. catch (e) {
  132. name = doc.getElementsByTagName('title')[0].firstChild.nodeValue;
  133. }
  134. if (name) {
  135. this.setName(name);
  136. }
  137. }
  138. var options = {};
  139. OpenLayers.Util.extend(options, this.formatOptions);
  140. if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
  141. options.externalProjection = this.projection;
  142. options.internalProjection = this.map.getProjectionObject();
  143. }
  144. var format = new OpenLayers.Format.GeoRSS(options);
  145. var features = format.read(doc);
  146. for (var i=0, len=features.length; i<len; i++) {
  147. var data = {};
  148. var feature = features[i];
  149. // we don't support features with no geometry in the GeoRSS
  150. // layer at this time.
  151. if (!feature.geometry) {
  152. continue;
  153. }
  154. var title = feature.attributes.title ?
  155. feature.attributes.title : "Untitled";
  156. var description = feature.attributes.description ?
  157. feature.attributes.description : "No description.";
  158. var link = feature.attributes.link ? feature.attributes.link : "";
  159. var location = feature.geometry.getBounds().getCenterLonLat();
  160. data.icon = this.icon == null ?
  161. OpenLayers.Marker.defaultIcon() :
  162. this.icon.clone();
  163. data.popupSize = this.popupSize ?
  164. this.popupSize.clone() :
  165. new OpenLayers.Size(250, 120);
  166. if (title || description) {
  167. // we have supplemental data, store them.
  168. data.title = title;
  169. data.description = description;
  170. var contentHTML = '<div class="olLayerGeoRSSClose">[x]</div>';
  171. contentHTML += '<div class="olLayerGeoRSSTitle">';
  172. if (link) {
  173. contentHTML += '<a class="link" href="'+link+'" target="_blank">';
  174. }
  175. contentHTML += title;
  176. if (link) {
  177. contentHTML += '</a>';
  178. }
  179. contentHTML += '</div>';
  180. contentHTML += '<div style="" class="olLayerGeoRSSDescription">';
  181. contentHTML += description;
  182. contentHTML += '</div>';
  183. data['popupContentHTML'] = contentHTML;
  184. }
  185. var feature = new OpenLayers.Feature(this, location, data);
  186. this.features.push(feature);
  187. var marker = feature.createMarker();
  188. marker.events.register('click', feature, this.markerClick);
  189. this.addMarker(marker);
  190. }
  191. this.events.triggerEvent("loadend");
  192. },
  193. /**
  194. * Method: markerClick
  195. *
  196. * Parameters:
  197. * evt - {Event}
  198. */
  199. markerClick: function(evt) {
  200. var sameMarkerClicked = (this == this.layer.selectedFeature);
  201. this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
  202. for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
  203. this.layer.map.removePopup(this.layer.map.popups[i]);
  204. }
  205. if (!sameMarkerClicked) {
  206. var popup = this.createPopup();
  207. OpenLayers.Event.observe(popup.div, "click",
  208. OpenLayers.Function.bind(function() {
  209. for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
  210. this.layer.map.removePopup(this.layer.map.popups[i]);
  211. }
  212. }, this)
  213. );
  214. this.layer.map.addPopup(popup);
  215. }
  216. OpenLayers.Event.stop(evt);
  217. },
  218. /**
  219. * Method: clearFeatures
  220. * Destroy all features in this layer.
  221. */
  222. clearFeatures: function() {
  223. if (this.features != null) {
  224. while(this.features.length > 0) {
  225. var feature = this.features[0];
  226. OpenLayers.Util.removeItem(this.features, feature);
  227. feature.destroy();
  228. }
  229. }
  230. },
  231. CLASS_NAME: "OpenLayers.Layer.GeoRSS"
  232. });