Generating KML / XML on the fly in Ruby on Rails June 7th, 2007
KML can now be used both in Google Maps and Google Earth.
I wrote a simple KML generator using Builder and Rails.
I also used an RSS parser to read geographic coords from an RSS feed which is in use now at Baleen.org, but that is a code story for another day..
I wanted to post some code snips from the project, cause it's cool.
The view "kml.rxml"
xml = Builder::XmlMarkup.new(:indent => 2)
xml.instruct! :xml
xml.kml( "kmlns" => "http://earth.google.com/kml/2.1" ) do
xml.Document {
xml.name("someplaces")
xml.description("someplaces.org locator")
xml.Style( "id" => "highlight" ) {
xml.IconStyle {
xml.Icon {
xml.href("http://URL to transparent 32x32 png image.png")
}
}
}
Place.find_all.each do |p|
xml.Place {
xml.name(p.tooltip)
xml.description {
x=p.popup.dup
xml.cdata!("#{x.slice!(1..250)}<br><a href=#{p.ext_url}>..Read more</a>")
}
xml.styleUrl("#highlight")
xml.Point {
xml.coordinates("#{p.x},#{p.y},4")
}
}
end
}
end
In the controller, you must specify the following headers.
def kml
render_without_layout
@headers["Content-Type"] = "application/vnd.google-earth.kml+xml kml; charset=utf-8"
@headers["Content-Disposition"] = "attachment; filename=someplaces.kml"
end
Here is the migration I used: "001_create_places.rb"
For storing latitude/longitude and a few other useful Google Earth / Maps data to be used in the KML
class CreatePlaces < ActiveRecord::Migration
def self.up
create_table :places do |t|
t.column :x, :string
t.column :y, :string
t.column :ext_url, :string
t.column :tooltip, :string
t.column :popup, :text
end
end
def self.down
drop_table :places
end
end
Use a route to behave as a downloadable file.
map.connect '/someplaces.kml', :controller => 'places', :action => 'kml'
That's all you need to start generating KML on the fly with Rails. Have fun!Bonus Code !! Example Javascript for Google Maps API that loads up KML from an URL.
var geoXml = new GGeoXml("http://URL to someplaces.kml");
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(37.12755479789267,-112.7487179140864),3);
map.setMapType(new GMapType([G_SATELLITE_MAP.getTileLayers()[0]], G_NORMAL_MAP.getProjection(), "test", {}));
map.addOverlay(geoXml);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
}
}
Also, make sure you have 2.x as the version string where you delcare the Javascript, otherwise your placemarks might go missing!
<script src="http://maps.google.com/maps?file=api&v=2.x&key=YOURKEY>
Leave a comment if this tutorial helped you. Thanks!
Rails conf 2007! May 16th, 2007
Woo! Rails Conf this weekend here in Portland, Oregon. I'm so excited my company paid for us to go to this. I've been working on some stuff for Baleen.org that I'll be making a post about here soon. They are riding their bikes to Costa Rica for the SOS children's villages. I've programmed up some rails XML magic to read in RSS and output KML for a google map/earth that tracks their current location. Pretty cool, cause I'm such an XML junkie. Anyway, I'm going to post some snippits from this for all you XML/KML fans out there.