GeoRSS with Ruby on Rails

Adding GPS coordinates to an RSS feed.

I keep the latitude and longitude for each of my posts. My blog’s RSS feed is generated from the list of posts. Until now it was a plain simple RSS feed but why not enhance the RSS feed with the post’s coordinates? They are there anyway and GeoRSS is widely adopted these days.

What is GeoRSS?

GeoRSS is a way to encode location information in RSS feeds. This can be as simple as adding one small XML element to your existing feed. GeoRSS feeds are designed to be consumed by geographic software such as map generators and by doing so your feed can be used in new and diverse ways: pinpointing on a map, finding other posts close to this one, etc.

What we need

Generating the feed is out of scope, but we’ll add location awareness to our existing feed. See Larry MyersHow To Generate RSS Feeds with Rails article to get started.

We’ll need to add a single XML element to each item of the feed with the latitude and longitude coordinates for each post.

<georss:point>50.8803605 4.7004394</georss:point>

In Ruby on Rails the syntax looks a little strange as we need a way to add the ‘georss:point’ XML element and Ruby doesn’t like the colon in there (see Ruby symbols).

xml.georss :point do
  xml.text! post.latitude.to_s  + ' ' + post.longitude.to_s

Next, we add the GeoRSS namespace to the top of the feed to tell the feed parsers what we mean.

xml.rss (:version => "2.0", "xmlns:georss" => "")

My location aware RSS feed

The full view template should now resemble the following:

xml.instruct! :xml, :version=>"2.0"
xml.rss (:version => "2.0", "xmlns:georss" => "") { {
    xml.title("My GeoRSS feed")"")
    xml.description("My posts enhanced with location info")
    for post in @posts
      xml.item do
        xml.description(post.content)" (#{})")
        xml.pubDate(post.created_at.strftime("%a, %d %b %Y %H:%M:%S %z")) post_permalink_url(post.permalink) )
        xml.guid( post_permalink_url(post.permalink) )
        xml.georss :point do
          xml.text! post.latitude.to_s  + ' ' + post.longitude.to_s

That’s it. Not too exciting at first but enter your RSS feed URL in Google Maps and see how easy it is to mark each post on a map!