Coder Social home page Coder Social logo

google_distance_matrix's Introduction

GoogleDistanceMatrix

Build Status

Ruby client for the Google Distance Matrix API.

This lib makes Google's distance matrix API easy to work with, allowing you to set up some origins and destinations and pull the distance matrix from Google.

Once you have the matrix you can fetch all routes from a given origin or to a given destination.

Examples

Set up a matrix to work with

matrix = GoogleDistanceMatrix::Matrix.new

Create some places to be used as origins or destinations for the matrix

lat_lng = GoogleDistanceMatrix::Place.new lng: 12, lat: 12
address = GoogleDistanceMatrix::Place.new address: "My address, Oslo"
dest_address = GoogleDistanceMatrix::Place.new address: "Home, Oppegaard"

# Just an example of an object responding to lat & lng.
# Point class isn't included in this gem, but feel free to
# create your own point class or use something like https://github.com/nofxx/georuby
point_dest = Point.new lat: 1, lng: 2
dest_object = GoogleDistanceMatrix::Place.new point_dest

Add places to matrix's origins and destinations

matrix.origins << lat_lng << address
matrix.destinations << dest_address << dest_object

# Added objects will be wrapped in a place automatically, so you may skip manyally creating Places.
another_point = Point.new lat: 1, lng: 3
matrix.origins << another_point

Do some configuration - see GoogleDistanceMatrix.configure_defaults as well.

matrix.configure do |config|
  config.mode = 'driving'
  config.avoid = 'tolls'

  # To build signed URLs to use with a Google Business account.
  config.google_business_api_client_id = "123"
  config.google_business_api_private_key = "your-secret-key"

  # If you have an API key, you can specify that as well.
  config.google_api_key = "YOUR_API_KEY"
end

Get the data for the matrix

matrix.data returns the data, loaded from Google, for this matrix.

It is a multi dimensional array. Rows are ordered according to the values in the origins. Each row corresponds to an origin, and each element within that row corresponds to a pairing of the origin with a destination.

Query the matrix

Returns an array of Google::DistanceMatrix::Route, all having given origin or destination.

matrix.routes_for dest_address

Returns Google::DistanceMatrix::Route with given origin and destination

matrix.route_for origin: lat_lng, destination: dest_address

# Returns the shortest route to given destination, either by distance or duration
matrix.shortest_route_by_distance_to(dest_address)
matrix.shortest_route_by_duration_to(dest_address)

# If your matrix is for driving and you provided a departure_time all Route objects within
# the matrix will have duration_in_traffic_in_seconds. We can query the matrix for this data as well:
matrix.shortest_route_by_duration_in_traffic_to(dest_address)

In cases where you built the place with an object (not hash with attributes) you may provide that object as well asking for routes. This is true for route_for and shortest_route_by_* as well.

matrix.routes_for point_dest # Returns routes for dest_object

You may call query methods with a bang, in which case it will fail with an error if not all of the routes in your result set for the called method are ok.

Installation

Add this line to your application's Gemfile:

gem 'google_distance_matrix'

And then execute:

$ bundle

Or install it yourself as:

$ gem install google_distance_matrix

Configuration

Configuration is done directly on a matrix or via GoogleDistanceMatrix.configure_defaults. Apart from configuration on requests it is also possible to provide your own logger class and set a cache.

Shorting the URL using encoded coordinates

Instead of lat and lng values in the URL it is possible to use encoded set of coordinates using the Encoded Polyline Algorithm. This is particularly useful if you have a large number of origin points, because the URL is significantly shorter when using an encoded polyline.

GoogleDistanceMatrix.configure_defaults do |config|
  config.use_encoded_polylines = true
end

Request cache

Given Google's limit to the service you may have the need to cache requests. This is done by simply using URL as cache keys. Cache we'll accept should provide a default ActiveSupport::Cache::Store interface.

GoogleDistanceMatrix.configure_defaults do |config|
  config.cache = ActiveSupport::Cache.lookup_store :your_store, {
    expires_in: 12.hours
    # ..or other options you like for your store
  }
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

google_distance_matrix's People

Contributors

thhermansen avatar rpocklin avatar lsanwick avatar brauliomartinezlm avatar mintyfresh avatar gudleik avatar iurifq avatar justisb avatar mask8 avatar w8m8 avatar zackchandler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.