Getting Google Earth & rails or merb talking

Posted by Shoaib on January 09, 2008

In this post I’ll demonstrate how your rails application can be made to talk to Google Earth. As a simple example, our rails app will put a cross hair in the center of the Google Earth window, every time the camera moves.

create a rails app rails ge_01

add a new mimetype for kml in config/initializers/mime_types.rb

Mime::Type.register "application/vnd.google-earth.kml+xml", :kml

create a lesson_one controller & index action/view; you may use the controller generator

setup database (sqlite3 will do)

create a google earth network link as a kml this can be done in a number of ways: you can create a kml file by hand or create a builder template to generate the kml

create network_link action which can be given to your clients

create a builder template (network_link.kml.builder)

server = url_for :controller => 'lesson_one',  :only_path => false
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
xml.Document {
  xml.name("Pass parameters to my Rails app")
  xml.open(1)
  xml.visible(1)
  xml.NetworkLink {
    xml.name("My rails app  being passed parameters")
    xml.open(1)
    xml.visibility(0)
    xml.Link {
      xml.href("#{server}")
      xml.viewRefreshMode("onStop")
      xml.viewRefreshTime(0.5)
      xml.viewFormat("BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]&CENTRE=[lookatLon],[lookatLat]")
      }
    }
  }
}

edit the index action in the lesson_one controller to capture the parameters from Google Earth in instance variables holding the Longitude and Latitude values for the centre

create an index.kml.builder template to respond to the request from Google Earth. Here we’ll use instance variables to create a marker for that point on the map.

text =    "
Centre Lng: #{@centre[0]}
Centre Lat: #{@centre[1]}
X Min: #{@bbox[0]}
Y Min: #{@bbox[1]}
X Max: #{@bbox[2]}
Y Max: #{@bbox[3]}
"

xml.instruct! :xml
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") do
  xml.Document {
    xml.Placemark {
      xml.Snippet(:maxLines => "9") {
        xml.cdata!(text)
      }
      xml.name("cross-hair")
      xml.Style {
        xml.LabelStyle {
          xml.scale(0)
        }
        xml.IconStyle {
          xml.color("ffefebde")
          xml.Icon {
            xml.href("root://icons/palette-3.png")
            xml.x(128)
            xml.y(32)
            xml.w(32)
            xml.h(32)
          }
        }
      }
      xml.Point {
        xml.coordinates("#{@centre[0]}, #{@centre[1]}");
      }
    }
  }
end

All this works pretty much unchanged in merb. One only needs to register the kml mime type by adding this to config/merb_init.rb:

Merb.add_mime_type(:kml, :to_kml, %w[application/vnd.google-earth.kml+xml], :Encoding => "UTF-8")

Time permitting I’ll try to make a screen-cast of this.
ge_rails

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. José A. Beltran Tue, 20 Apr 2010 18:50:29 UTC

    Muy Bueno…

Comments