A simple Gem to use the Google Maps distance API from Ruby
Switch branches/tags
Nothing to show
Pull request Compare This branch is 8 commits behind railsbros-dirk:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Sometimes you need to calculate the directions between two different points via Google Maps on your server-side and not on the client. The Google Maps API for that is dead simple and due to this I wrapped calling the API in this little gem.

The gem talks to the Google Maps Directions Web Service API. Detailed documentation for that can be found here This API allows up to 2.500 direction requests. At the current version there is no way to use the non-public interface for Google Premium Maps customers. If you want that feature right now drop me a patch or wait until I need it too (which might not take too long anyway).

One last thing for the interested: The gem consumes the JSON response format from Google API and parses it through the Ruby Yajl JSON parser.


Here is an example of how to use the Gem. As you can see, it is really more than simple:

require 'rubygems'
require 'gmaps_directions'

routes = GmapsDirections::API.find_directions :from => "1 Infinite Loop, Cupertino",
                                             :to => "1200 Park Avenue, Emmerville"
route = routes.first

route.duration.should           == 3482
route.formatted_duration.should == "58 mins"
route.distance.should           == 84827
route.formatted_distance.should == "84.8 km"
route.start_address.should      == "1 Infinite Loop, Cupertino, CA 95014, USA"
route.end_address.should        == "1200 Park Ave, Emeryville, CA 94608, USA"
route.start_location.should     == { "lng" => -122.03125, "lat" => 37.3317 }
route.end_location.should       == { "lat" => 37.8317100, "lng" => -122.2833000 }
route.status.should             == "OK"


Despite the fact that you cannot provide an API-Key I already built some kind of configuration. In detail you can configure the following options when talking to Google:

  • The type of transportation to use when calculate the direction ('driving', 'walking', 'bicycling')

  • The unit system to use ('metric', 'imperial')

  • The language in which the results is outputted (this includes number formats)

  • The availability of a GPS-sensor

  • Should there alternative routes be calculated

The following snippet shows how to configure those options and what are the defaults:

GmapsDirections::Config.mode         = :driving
GmapsDirections::Config.units        = :metric
GmapsDirections::Config.language     = :en
GmapsDirections::Config.sensor       = false
GmapsDirections::Config.alternatives = false

Contributing to gmaps_directions

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright © 2011 Dirk Breuer. See LICENSE.txt for further details.