Over a million developers have joined DZone.

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

DZone's Guide to

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

Free Resource
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]

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("",pos1) unless pos1 == nil
pos1 = html.index(">",pos2+6)+1 unless pos2 == nil
pos2 = html.index("",pos2+1) -1 unless pos2 == nil
ret = ""
if pos1 != nil and pos2 != nil
        ret = html[pos1..pos2].gsub!(" ", " ")
        ret = "0"
html = nil
p ret

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}