GeoRSS.js 8.4 KB

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