Suffix

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
end

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" => "http://www.georss.org/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" => "http://www.georss.org/georss") {
  xml.channel {
    xml.title("My GeoRSS feed")
    xml.link("http://www.example.com")
    xml.description("My posts enhanced with location info")
    xml.language('en-us')
    for post in @posts
      xml.item do
        xml.title(post.title)
        xml.description(post.content)
        xml.author("none@none.be (#{post.author})")
        xml.pubDate(post.created_at.strftime("%a, %d %b %Y %H:%M:%S %z"))
        xml.link( 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
        end
      end
    end
    }
}

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!