Adding GeoRSS to OpenLayers Vector Layer via jQuery
For a number of my OpenLayers projects I create a vector layer to hold markers and handle popups et al. I’ve found this more flexible than a marker layer, especially if you want to futz around with particular features later. I add a vector layer with a different vector type for each type of thing I might want to slap in there. Something like this:
/** * Add markers (vector) to the map. * Also removes popups and selects added feature. * @param {float}lon * @param {float}lat * @param featuretype: the type of feature/marker (0=address,1=facility,2=identify) * @param label: the content to put in the popup * @param {boolean}clearFeatures: clear all other makers of same featuretype * @param {boolean}zoomToFeature: zoom to the added marker if not in map view/zoomed out too far * @param {boolean}selectFeature: select the feature, opening popup */ functionaddMarker(lon, lat, featuretype, label, clearFeatures, zoomToFeature, selectFeature) { // remove old features of same type if clearOthers is true if (clearFeatures) { feats = markerLayer.features; for(i = 0; i < feats.length; i++) { if (feats[i].attributes.type == featuretype) markerLayer.removeFeatures(feature); } }
// Add new feature point = new OpenLayers.Geometry.Point(lon, lat); OpenLayers.Projection.transform(point, map.displayProjection, map.getProjectionObject()); label = '<div>' + label + "</div>"; label += '<br /><div><a href="#" onclick="zoomTo(' + point.x + ', ' + point.y + ', 8); return false">Zoom to Feature</a></div>'; feature = new OpenLayers.Feature.Vector(point, {type: featuretype, label: label} );
markerLayer.addFeatures(feature);
// zoom to feature if zoomToFeature is true if (zoomToFeature) { if (!feature.onScreen() || map.getZoom() < 8) { zoomToLonLat (lon, lat, 8); } }
// open feature popup if map width greater than 500 pixels (hides on portables) if (selectFeature && $("#map").width() > 500 ) selectControl.select(feature); }
Now let’s use jQuery to process the Yahoo Traffic GeoRSS feed and add the locations to the vector layer. When it comes to making AJAX requests and processing the results on the client side, I haven’t found anything easier or more intuitive than jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Add traffic to marker layer $.ajax({ type: "GET", url: "yahoo_traffic_proxy", dataType: "xml", success: function(xml) { $(xml).find('[nodeName=item]').each(function(){ var lat = $(this).find("[nodeName=geo:lat]").text(); var lon = $(this).find("[nodeName=geo:long]").text(); var title = $(this).find('[nodeName=title]').text(); var description = $(this).find('[nodeName=description]').text(); addMarker(lon, lat, 3, "<strong>TRAFFIC/INCIDENT INFORMATION</strong><br />" + title + "<br />" + description, false, false, false); }); } });
And that’s it. Note to avoid the dread XSS error, you’ll need to proxy the GeoRSS feed. For each “item” in the RSS feed, we’re pulling out the location, the title, and the description, and adding the features to the vector layer on our map.
Like there's ever fast traffic there.
One important thing to note. When using find in jQuery on XML, use:
1
var lat = $(this).find("[nodeName=geo:lat]").text();
And not:
1
var lat = $(this).find("geo//:lat").text();
Escaped strings in jQuery find for XML don’t work in Webkit based browsers, so you’d have to do something hacky like “geo//:lat, lat”. Use nodeName, and you’re golden.