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