Posted on August 27, 2007
Do you want driving directions on your website but don’t want to mess around with the Google Maps api?
Well, you can easily implement Google driving directions by calling the url:
http://maps.google.com/maps?saddr=start_address&daddr=dest_address&hl=en
So, in Rails you could do something like this:
<%= button_to_function "Get Driving Directions", "window.open(\"http://maps.google.com/maps?saddr=#{URI.escape(source_address)}&daddr=#{URI.escape(destination_address)}&hl=en\")" %>
Where source_address and destination_address are string variables containing things like:
London -> Manchester
SE5 0LL -> NW6 6RJ
221b Baker St -> 10 Downing Street
This method handles all the geocoding for ya – and is way simpler than messing with the GDirections api object.
Filed under: Ruby on Rails |
Posted on August 24, 2007
Serving a KML file in Ruby on Rails is way easier than doing it in asp.net.
Here’s how I do it for my little display kml on a google map application.
First of all, you want to trick Google into thinking that the kml file is just a regular file with the extension “kml”. I do this by using a custom route in routes.rb that looks a little something like this:
map.connect ':controller/:action/:ignore_this_bit/:uuid.:format
#The :ignore_this_bit is optional if you want to get around the google
#caching issue detailed below
So that means you could serve the file with a url like this:
http://www.my_domain.com/my_controller/get_kml/'random_number'/11.kml
If you put a random number in there then you’ll get around Googles 4 minute cache.
Next you create a controller method that will spit out the
KML and render it to the browser using the send_data function like the example detailed below. (I load my kml data using a
UUID by the way – but you can do it any way you want):
def get_kml
@kml_data = KmlData.find_by_uuid(params[:uuid])
if @kml_data.kml
send_data @kml_data.kml
else
render :text => 'Kml is empty'
end
end
The @kml_data.kml variable contains the raw xml formatted kml.
The real guts of the function is the send_data function. This is rubys way of sending a file to the browser, which is how google expects to view the file.
And that’s it! Piece of cake!
Filed under: Ruby on Rails |
Tagged with: kml |