1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Layer/Markers.js
8 * @requires OpenLayers/Request/XMLHttpRequest.js
12 * Class: OpenLayers.Layer.GeoRSS
13 * Add GeoRSS Point features to your map.
16 * - <OpenLayers.Layer.Markers>
17 * - <OpenLayers.Layer>
19 OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
23 * {String} store url of text file
29 * {Array(<OpenLayers.Feature>)}
34 * APIProperty: formatOptions
35 * {Object} Hash of options which should be passed to the format when it is
36 * created. Must be passed in the constructor.
41 * Property: selectedFeature
42 * {<OpenLayers.Feature>}
44 selectedFeature: null,
48 * {<OpenLayers.Icon>}. This determines the Icon to be used on the map
49 * for this GeoRSS layer.
54 * APIProperty: popupSize
55 * {<OpenLayers.Size>} This determines the size of GeoRSS popups. If
56 * not provided, defaults to 250px by 120px.
61 * APIProperty: useFeedTitle
62 * {Boolean} Set layer.name to the first <title> element in the feed. Default is true.
67 * Constructor: OpenLayers.Layer.GeoRSS
68 * Create a GeoRSS Layer.
75 initialize: function(name, location, options) {
76 OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name, options]);
77 this.location = location;
85 // Warning: Layer.Markers.destroy() must be called prior to calling
86 // clearFeatures() here, otherwise we leak memory. Indeed, if
87 // Layer.Markers.destroy() is called after clearFeatures(), it won't be
88 // able to remove the marker image elements from the layer's div since
89 // the markers will have been destroyed by clearFeatures().
90 OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
97 * Start the load of the RSS data. Don't do this when we first add the layer,
98 * since we may not be visible at any point, and it would therefore be a waste.
100 loadRSS: function() {
102 this.events.triggerEvent("loadstart");
103 OpenLayers.Request.GET({
105 success: this.parseData,
114 * If layer is visible and RSS has not been loaded, load RSS.
118 * zoomChanged - {Object}
121 moveTo:function(bounds, zoomChanged, minor) {
122 OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments);
123 if(this.visibility && !this.loaded){
130 * Parse the data returned from the Events call.
133 * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
135 parseData: function(ajaxRequest) {
136 var doc = ajaxRequest.responseXML;
137 if (!doc || !doc.documentElement) {
138 doc = OpenLayers.Format.XML.prototype.read(ajaxRequest.responseText);
141 if (this.useFeedTitle) {
144 name = doc.getElementsByTagNameNS('*', 'title')[0].firstChild.nodeValue;
147 name = doc.getElementsByTagName('title')[0].firstChild.nodeValue;
156 OpenLayers.Util.extend(options, this.formatOptions);
158 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
159 options.externalProjection = this.projection;
160 options.internalProjection = this.map.getProjectionObject();
163 var format = new OpenLayers.Format.GeoRSS(options);
164 var features = format.read(doc);
166 for (var i=0, len=features.length; i<len; i++) {
168 var feature = features[i];
170 // we don't support features with no geometry in the GeoRSS
171 // layer at this time.
172 if (!feature.geometry) {
176 var title = feature.attributes.title ?
177 feature.attributes.title : "Untitled";
179 var description = feature.attributes.description ?
180 feature.attributes.description : "No description.";
182 var link = feature.attributes.link ? feature.attributes.link : "";
184 var location = feature.geometry.getBounds().getCenterLonLat();
187 data.icon = this.icon == null ?
188 OpenLayers.Marker.defaultIcon() :
191 data.popupSize = this.popupSize ?
192 this.popupSize.clone() :
193 new OpenLayers.Size(250, 120);
195 if (title || description) {
196 // we have supplemental data, store them.
198 data.description = description;
200 var contentHTML = '<div class="olLayerGeoRSSClose">[x]</div>';
201 contentHTML += '<div class="olLayerGeoRSSTitle">';
203 contentHTML += '<a class="link" href="'+link+'" target="_blank">';
205 contentHTML += title;
207 contentHTML += '</a>';
209 contentHTML += '</div>';
210 contentHTML += '<div style="" class="olLayerGeoRSSDescription">';
211 contentHTML += description;
212 contentHTML += '</div>';
213 data['popupContentHTML'] = contentHTML;
215 var feature = new OpenLayers.Feature(this, location, data);
216 this.features.push(feature);
217 var marker = feature.createMarker();
218 marker.events.register('click', feature, this.markerClick);
219 this.addMarker(marker);
221 this.events.triggerEvent("loadend");
225 * Method: markerClick
230 markerClick: function(evt) {
231 var sameMarkerClicked = (this == this.layer.selectedFeature);
232 this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
233 for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
234 this.layer.map.removePopup(this.layer.map.popups[i]);
236 if (!sameMarkerClicked) {
237 var popup = this.createPopup();
238 OpenLayers.Event.observe(popup.div, "click",
239 OpenLayers.Function.bind(function() {
240 for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
241 this.layer.map.removePopup(this.layer.map.popups[i]);
245 this.layer.map.addPopup(popup);
247 OpenLayers.Event.stop(evt);
251 * Method: clearFeatures
252 * Destroy all features in this layer.
254 clearFeatures: function() {
255 if (this.features != null) {
256 while(this.features.length > 0) {
257 var feature = this.features[0];
258 OpenLayers.Util.removeItem(this.features, feature);
264 CLASS_NAME: "OpenLayers.Layer.GeoRSS"