Module: GoogleMapsService::Directions
- Included in:
- Client
- Defined in:
- lib/google_maps_service/directions.rb
Overview
Performs requests to the Google Maps Directions API.
Instance Method Summary collapse
-
#directions(origin: nil, destination: nil, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil) ⇒ Object
Get directions between an origin point and a destination point.
Instance Method Details
#directions(origin: nil, destination: nil, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil) ⇒ Object
Get directions between an origin point and a destination point.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/google_maps_service/directions.rb', line 39 def directions(origin: nil, destination: nil, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil) params = { origin: _convert_waypoint(origin), destination: _convert_waypoint(destination) } if mode # NOTE(broady): the mode parameter is not validated by the Maps API # server. Check here to prevent silent failures. unless ["driving", "walking", "bicycling", "transit"].include?(mode) raise ArgumentError, "Invalid travel mode." end params[:mode] = mode end if waypoints waypoints = GoogleMapsService::Convert.as_list(waypoints) waypoints = waypoints.map { |waypoint| _convert_waypoint(waypoint) } waypoints = ["optimize:true"] + waypoints if optimize_waypoints params[:waypoints] = GoogleMapsService::Convert.join_list("|", waypoints) end params[:alternatives] = "true" if alternatives params[:avoid] = GoogleMapsService::Convert.join_list("|", avoid) if avoid params[:language] = language if language params[:units] = units if units params[:region] = region if region params[:departure_time] = GoogleMapsService::Convert.time(departure_time) if departure_time params[:arrival_time] = GoogleMapsService::Convert.time(arrival_time) if arrival_time if departure_time and arrival_time raise ArgumentError, "Should not specify both departure_time and arrival_time." end params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference return get("/maps/api/directions/json", params)[:routes] end |