Created
May 21, 2018 13:04
-
-
Save johnmeehan/c7600d7c0536576a25a01a998562adeb to your computer and use it in GitHub Desktop.
Google Distance Matrix and Directions API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/google_map/directions.rb | |
# Calls the google maps directions api | |
# https://developers.google.com/maps/documentation/directions/intro | |
# Useage: | |
# gm = GoogleMap::Directions.new({origin: '52.662377,-8.630171', destination: '52.635720, -8.654714'}) | |
# gm.call | |
module GoogleMap | |
class Directions | |
include HTTParty | |
base_uri 'https://maps.googleapis.com/maps/api' | |
default_params key: Rails.configuration.x.maps.api_browser_key | |
format :json | |
debug_output $stdout | |
attr_accessor :response | |
def initialize(args) | |
@units = args[:units] || 'metric' | |
@mode = args[:mode] || 'driving' | |
@travel_model = args[:travel_model] || 'best_guess' | |
@alternatives = args[:alternatives] || 'false' | |
@origin = args[:origin] | |
@destination = args[:destination] | |
# @waypoints = args[:waypoints] # outside commitments & phone meetings? | |
end | |
def call | |
@response = self.class.get('/directions/json', query: options) | |
return unless response.code == 200 | |
format_directions_result | |
end | |
private | |
def options | |
{ | |
units: @units, | |
mode: @mode, | |
travel_model: @travel_model, | |
alternatives: @alternatives, | |
origin: @origin, | |
destination: @destination | |
} | |
end | |
def format_directions_result | |
DirectionsResult.new({ | |
distance: response.dig('distance', 'value'), | |
distance_text: response.dig('distance', 'text'), | |
duration: response.dig('duration', 'value'), | |
duration_text: response.dig('duration', 'text'), | |
start_address: response.dig('start_address'), | |
end_address: response.dig('end_address'), | |
}) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/google_map/directions_result.rb | |
module GoogleMap | |
class DirectionsResult | |
attr_accessor :distance, :distance_text, :duration, :duration_text, :start_address, :end_address | |
def initialize(distance:, distance_text:, duration:, duration_text:, start_address:, end_address:) | |
@distance = distance | |
@distance_text = distance_text | |
@duration = duration | |
@duration_text = duration_text | |
@start_address = start_address | |
@end_address = end_address | |
end | |
def to_hash | |
{ | |
distance: distance, | |
distance_text: distance_text, | |
duration: duration, | |
duration_text: duration_text, | |
start_address: start_address, | |
end_address: end_address | |
} | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/google_map/distance_matrix.rb | |
# Calls the google maps distance matrix api | |
# https://developers.google.com/maps/documentation/distance-matrix/intro#DistanceMatrixRequests | |
# limitations: | |
# max 25 origins or 25 destinations per request | |
# max 100 elements per request | |
# number of elements = number of origins times number of destinations | |
# Useage: | |
# gm = GoogleMap::DistanceMatrix.new({origins: ['52.662377,-8.630171'], destinations: ['52.635720, -8.654714', '52.662026, -8.550382']}) | |
# gm.call | |
module GoogleMap | |
class DistanceMatrix | |
include HTTParty | |
base_uri 'https://maps.googleapis.com/maps/api' | |
default_params key: Rails.configuration.x.maps.api_browser_key | |
format :json | |
debug_output $stdout | |
attr_accessor :key, :units, :mode, :travel_model, :options | |
def initialize(args) | |
@units = args[:units] || 'metric' | |
@mode = args[:mode] || 'driving' | |
@travel_model = args['travel_model'] || 'best_guess' | |
@origins = args[:origins] | |
@destinations = args[:destinations] | |
end | |
def call | |
endpoint = '/distancematrix/json' | |
self.class.get(endpoint, query: options) | |
end | |
# NB Format --> origins=41.43206,-81.38992|-33.86748,151.20699 | |
def origins | |
@origins.join('|') | |
end | |
def destinations | |
@destinations.join('|') | |
end | |
def options | |
{ | |
units: @units, | |
mode: @mode, | |
travel_model: @travel_model, | |
origins: origins, | |
destinations: destinations | |
} | |
end | |
end | |
end | |
## SAMPLE RESPONSE | |
# { | |
# "destination_addresses" => [ | |
# [0] "St Nessan's Rd, Dooradoyle, Limerick, Ireland", | |
# [1] "L1121, Newtown, Co. Limerick, Ireland" | |
# ], | |
# "origin_addresses" => [ | |
# [0] "Henry St, Limerick, Ireland" | |
# ], | |
# "rows" => [ | |
# [0] { | |
# "elements" => [ | |
# [0] { | |
# "distance" => { | |
# "text" => "5.5 km", | |
# "value" => 5456 | |
# }, | |
# "duration" => { | |
# "text" => "12 mins", | |
# "value" => 715 | |
# }, | |
# "status" => "OK" | |
# }, | |
# [1] { | |
# "distance" => { | |
# "text" => "6.4 km", | |
# "value" => 6424 | |
# }, | |
# "duration" => { | |
# "text" => "14 mins", | |
# "value" => 869 | |
# }, | |
# "status" => "OK" | |
# } | |
# ] | |
# } | |
# ], | |
# "status" => "OK" | |
# } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/google_map.rb | |
require "google_map/directions" | |
require "google_map/distance_matrix" | |
require "google_map/directions_result" | |
module GoogleMap | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment