Text.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/Format/Text.js
  8. * @requires OpenLayers/Request/XMLHttpRequest.js
  9. */
  10. /**
  11. * Class: OpenLayers.Layer.Text
  12. * This layer creates markers given data in a text file. The <location>
  13. * property of the layer (specified as a property of the options argument
  14. * in the <OpenLayers.Layer.Text> constructor) points to a tab delimited
  15. * file with data used to create markers.
  16. *
  17. * The first row of the data file should be a header line with the column names
  18. * of the data. Each column should be delimited by a tab space. The
  19. * possible columns are:
  20. * - *point* lat,lon of the point where a marker is to be placed
  21. * - *lat* Latitude of the point where a marker is to be placed
  22. * - *lon* Longitude of the point where a marker is to be placed
  23. * - *icon* or *image* URL of marker icon to use.
  24. * - *iconSize* Size of Icon to use.
  25. * - *iconOffset* Where the top-left corner of the icon is to be placed
  26. * relative to the latitude and longitude of the point.
  27. * - *title* The text of the 'title' is placed inside an 'h2' marker
  28. * inside a popup, which opens when the marker is clicked.
  29. * - *description* The text of the 'description' is placed below the h2
  30. * in the popup. this can be plain text or HTML.
  31. *
  32. * Example text file:
  33. * (code)
  34. * lat lon title description iconSize iconOffset icon
  35. * 10 20 title description 21,25 -10,-25 http://www.openlayers.org/dev/img/marker.png
  36. * (end)
  37. *
  38. * Inherits from:
  39. * - <OpenLayers.Layer.Markers>
  40. */
  41. OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
  42. /**
  43. * APIProperty: location
  44. * {String} URL of text file. Must be specified in the "options" argument
  45. * of the constructor. Can not be changed once passed in.
  46. */
  47. location:null,
  48. /**
  49. * Property: features
  50. * {Array(<OpenLayers.Feature>)}
  51. */
  52. features: null,
  53. /**
  54. * APIProperty: formatOptions
  55. * {Object} Hash of options which should be passed to the format when it is
  56. * created. Must be passed in the constructor.
  57. */
  58. formatOptions: null,
  59. /**
  60. * Property: selectedFeature
  61. * {<OpenLayers.Feature>}
  62. */
  63. selectedFeature: null,
  64. /**
  65. * Constructor: OpenLayers.Layer.Text
  66. * Create a text layer.
  67. *
  68. * Parameters:
  69. * name - {String}
  70. * options - {Object} Object with properties to be set on the layer.
  71. * Must include <location> property.
  72. */
  73. initialize: function(name, options) {
  74. OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
  75. this.features = new Array();
  76. },
  77. /**
  78. * APIMethod: destroy
  79. */
  80. destroy: function() {
  81. // Warning: Layer.Markers.destroy() must be called prior to calling
  82. // clearFeatures() here, otherwise we leak memory. Indeed, if
  83. // Layer.Markers.destroy() is called after clearFeatures(), it won't be
  84. // able to remove the marker image elements from the layer's div since
  85. // the markers will have been destroyed by clearFeatures().
  86. OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
  87. this.clearFeatures();
  88. this.features = null;
  89. },
  90. /**
  91. * Method: loadText
  92. * Start the load of the Text data. Don't do this when we first add the layer,
  93. * since we may not be visible at any point, and it would therefore be a waste.
  94. */
  95. loadText: function() {
  96. if (!this.loaded) {
  97. if (this.location != null) {
  98. var onFail = function(e) {
  99. this.events.triggerEvent("loadend");
  100. };
  101. this.events.triggerEvent("loadstart");
  102. OpenLayers.Request.GET({
  103. url: this.location,
  104. success: this.parseData,
  105. failure: onFail,
  106. scope: this
  107. });
  108. this.loaded = true;
  109. }
  110. }
  111. },
  112. /**
  113. * Method: moveTo
  114. * If layer is visible and Text has not been loaded, load Text.
  115. *
  116. * Parameters:
  117. * bounds - {Object}
  118. * zoomChanged - {Object}
  119. * minor - {Object}
  120. */
  121. moveTo:function(bounds, zoomChanged, minor) {
  122. OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments);
  123. if(this.visibility && !this.loaded){
  124. this.loadText();
  125. }
  126. },
  127. /**
  128. * Method: parseData
  129. *
  130. * Parameters:
  131. * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
  132. */
  133. parseData: function(ajaxRequest) {
  134. var text = ajaxRequest.responseText;
  135. var options = {};
  136. OpenLayers.Util.extend(options, this.formatOptions);
  137. if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
  138. options.externalProjection = this.projection;
  139. options.internalProjection = this.map.getProjectionObject();
  140. }
  141. var parser = new OpenLayers.Format.Text(options);
  142. var features = parser.read(text);
  143. for (var i=0, len=features.length; i<len; i++) {
  144. var data = {};
  145. var feature = features[i];
  146. var location;
  147. var iconSize, iconOffset;
  148. location = new OpenLayers.LonLat(feature.geometry.x,
  149. feature.geometry.y);
  150. if (feature.style.graphicWidth
  151. && feature.style.graphicHeight) {
  152. iconSize = new OpenLayers.Size(
  153. feature.style.graphicWidth,
  154. feature.style.graphicHeight);
  155. }
  156. // FIXME: At the moment, we only use this if we have an
  157. // externalGraphic, because icon has no setOffset API Method.
  158. /**
  159. * FIXME FIRST!!
  160. * The Text format does all sorts of parseFloating
  161. * The result of a parseFloat for a bogus string is NaN. That
  162. * means the three possible values here are undefined, NaN, or a
  163. * number. The previous check was an identity check for null. This
  164. * means it was failing for all undefined or NaN. A slightly better
  165. * check is for undefined. An even better check is to see if the
  166. * value is a number (see #1441).
  167. */
  168. if (feature.style.graphicXOffset !== undefined
  169. && feature.style.graphicYOffset !== undefined) {
  170. iconOffset = new OpenLayers.Pixel(
  171. feature.style.graphicXOffset,
  172. feature.style.graphicYOffset);
  173. }
  174. if (feature.style.externalGraphic != null) {
  175. data.icon = new OpenLayers.Icon(feature.style.externalGraphic,
  176. iconSize,
  177. iconOffset);
  178. } else {
  179. data.icon = OpenLayers.Marker.defaultIcon();
  180. //allows for the case where the image url is not
  181. // specified but the size is. use a default icon
  182. // but change the size
  183. if (iconSize != null) {
  184. data.icon.setSize(iconSize);
  185. }
  186. }
  187. if ((feature.attributes.title != null)
  188. && (feature.attributes.description != null)) {
  189. data['popupContentHTML'] =
  190. '<h2>'+feature.attributes.title+'</h2>' +
  191. '<p>'+feature.attributes.description+'</p>';
  192. }
  193. data['overflow'] = feature.attributes.overflow || "auto";
  194. var markerFeature = new OpenLayers.Feature(this, location, data);
  195. this.features.push(markerFeature);
  196. var marker = markerFeature.createMarker();
  197. if ((feature.attributes.title != null)
  198. && (feature.attributes.description != null)) {
  199. marker.events.register('click', markerFeature, this.markerClick);
  200. }
  201. this.addMarker(marker);
  202. }
  203. this.events.triggerEvent("loadend");
  204. },
  205. /**
  206. * Property: markerClick
  207. *
  208. * Parameters:
  209. * evt - {Event}
  210. */
  211. markerClick: function(evt) {
  212. var sameMarkerClicked = (this == this.layer.selectedFeature);
  213. this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
  214. for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
  215. this.layer.map.removePopup(this.layer.map.popups[i]);
  216. }
  217. if (!sameMarkerClicked) {
  218. this.layer.map.addPopup(this.createPopup());
  219. }
  220. OpenLayers.Event.stop(evt);
  221. },
  222. /**
  223. * Method: clearFeatures
  224. */
  225. clearFeatures: function() {
  226. if (this.features != null) {
  227. while(this.features.length > 0) {
  228. var feature = this.features[0];
  229. OpenLayers.Util.removeItem(this.features, feature);
  230. feature.destroy();
  231. }
  232. }
  233. },
  234. CLASS_NAME: "OpenLayers.Layer.Text"
  235. });