DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Get Google Maps Informations (distance And Time, French Version)

05.08.2007
| 5332 views |
  • submit to reddit
        Here a little script to get time and distance between two towns with google maps. You can change "voiture" by your langage word "car", or anything else.

this is a command line version : # ruby myscript.rb "Lyon" "Paris"
You can easily change it for a Ruby on Rails project :)
Maybie we can upgrade this script using regexp.

require "open-uri"
require "cgi"

# Command line arguments ... 
$*.each {|argu| argu.gsub!("\"","")}
# Defaults towns
src = "Lyon"
dest = "Paris"
# If we have fill two aguments ...
if $*.size == 2 then
        src = $*[0]
        dest = $*[1]
end

html = ""
url = "http://maps.google.com/maps?f=d&hl=fr&saddr=" + CGI.escape(src) + "&daddr="+ CGI.escape(dest)
# Read the html page
open(url) {|f|
        f.each_line {|line| html += line}
}
pos1 = html.index("voiture") # French version
pos2 = html.index("</td>",pos1) unless pos1 == nil
pos1 = html.index(">",pos2+6)+1 unless pos2 == nil
pos2 = html.index("</td>",pos2+1) -1 unless pos2 == nil
ret = ""
if pos1 != nil and pos2 != nil
        ret = html[pos1..pos2].gsub!(" ", " ")
else
        ret = "0"
end
html = nil
p ret