Google Directions Rest API

A rest API flow that wraps the Google Directions API.

This flow makes use of the Google Web Nodes (https://github.com/node-red/node-red-web-nodes/tree/master/google)

Prerequisites

  • First, you will need to install the Google Web Nodes.

      npm install node-red-node-google
    
  • The Google Directions API requires a Google Developer API key. You will need to create a project with Google in order to obtain one. (https://code.google.com/apis/console/)

  • This flow also contains additional swagger documentation. This is to be used with the Node Red Swagger Plugin (https://github.com/node-red/node-red-node-swagger)

      npm install node-red-node-swagger
    

Usage

The flow will create an HTTP endpoint for Google Directions API requests. The endpoint will direct the user's input to a Google Directions API request. You can make changes to the Google Directions node's configuration if you would like to include any information ahead of time, like API key for instance. All properties taken by the Google Directions node can be passed as msg level parameters. The Google Directions node is heavily documented, and details about possible parameters can be acquired via the info tab. All errors that occur will be relayed back to the user via the catch node.

[  
   {  
      "id":"e4daec3b.1b251",
      "type":"swagger-doc",
      "summary":"Google Directions",
      "description":"The Geocoding endpoint performs a request against the Google Geocoding API. This will convert an address into a set of lat/lon coordinate points.",
      "tags":"GoogleDirections",
      "consumes":"application/json",
      "produces":"application/json",
      "parameters":[  
         {  
            "name":"origin",
            "in":"query",
            "description":"The address or textual latitude/longitude value from which you wish to calculate directions. If you pass an address as a string, the Directions service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.",
            "required":true,
            "type":"string"
         },
         {  
            "name":"destination",
            "in":"query",
            "description":"The address or textual latitude/longitude value from which you wish to calculate directions. If you pass an address as a string, the Directions service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.",
            "required":true,
            "type":"string"
         },
         {  
            "name":"key",
            "in":"query",
            "description":"Google provided API key",
            "required":false,
            "type":"string"
         },
         {  
            "name":"mode",
            "in":"query",
            "description":"Specifies the mode of transport to use when calculating directions. Valid values and other request details are specified in <a href=\\\"https://developers.google.com/maps/documentation/directions/?csw=1#TravelModes\\\">Travel Modes</a>.",
            "required":false,
            "type":"string"
         },
         {  
            "name":"waypoints",
            "in":"query",
            "description":"Specifies an array of waypoints. Waypoints alter a route by routing it through the specified location(s). A waypoint is specified as either a latitude/longitude coordinate or as an address which will be geocoded. Waypoints are only supported for driving, walking and bicycling directions. (For more information on waypoints, see <a href=\\\"https://developers.google.com/maps/documentation/directions/?csw=1#Waypoints\\\">Using Waypoints</a>.)",
            "required":false,
            "type":"string"
         },
         {  
            "name":"alternatives",
            "in":"query",
            "description":"If set to true, specifies that the Directions service may provide more than one route alternative in the response. Note that providing route alternatives may increase the response time from the server.",
            "required":false,
            "type":"boolean"
         },
         {  
            "name":"avoid",
            "in":"query",
            "description":"Indicates that the calculated route(s) should avoid the indicated features. This parameter supports the following arguments:<br /> <i>tolls</i> - indicates that the calculated route should avoid toll roads/bridges.<br /><i>highways</i> - indicates that the calculated route should avoid highways.<br /><i>ferries</i> - indicates that the calculated route should avoid ferries.",
            "required":false,
            "type":"string"
         },
         {  
            "name":"language",
            "in":"query",
            "description":"Specifies the language in which to return results. See the <a href=\\\"https://developers.google.com/maps/faq#languagesupport\\\">list of supported domain languages</a>.",
            "required":false,
            "type":"string"
         },
         {  
            "name":"units",
            "in":"query",
            "description":"Specifies the unit system to use when displaying results. Valid values are specified in <a href=\\\"https://developers.google.com/maps/documentation/directions/?csw=1#UnitSystems\\\">Unit Systems</a>.",
            "required":false,
            "type":"string"
         },
         {  
            "name":"region",
            "in":"query",
            "description":"Specifies the region code, specified as a ccTLD (\\\"top-level domain\\\") two-character value. (For more information see <a href=\\\"https://developers.google.com/maps/documentation/directions/?csw=1#RegionBiasing\\\">Region Biasing</a> .)",
            "required":false,
            "type":"string"
         },
         {  
            "name":"departure_time",
            "in":"query",
            "description":"Specifies the desired time of departure. You can specify the time as an integer in seconds since midnight, January 1, 1970 UTC. Alternatively, you can specify a value of now, which sets the departure time to the current time (correct to the nearest second).",
            "required":false,
            "type":"integer"
         },
         {  
            "name":"arrival_time",
            "in":"query",
            "description":"Specifies the desired time of arrival for transit directions, in seconds since midnight, January 1, 1970 UTC. You can specify either departure_time or arrival_time, but not both.",
            "required":false,
            "type":"integer"
         },
         {  
            "name":"transit_mode",
            "in":"query",
            "description":"Specifies one or more preferred modes of transit. This parameter may only be specified for transit directions, and only if the request includes an API key or a Google Maps API for Work client ID. The parameter supports the following arguments:<br /><i>bus</i> - indicates that the calculated route should prefer travel by bus.<br /><i>subway</i> - indicates that the calculated route should prefer travel by subway.<br /><i>train</i> - indicates that the calculated route should prefer travel by train.<br /><i>tram</i> - indicates that the calculated route should prefer travel by tram and light rail.<br /><i>rail</i> - indicates that the calculated route should prefer travel by train, tram, light rail, and subway. This is equivalent to transit_mode=train|tram|subway.",
            "required":false,
            "type":"string"
         },
         {  
            "name":"transit_routing_preferences",
            "in":"query",
            "description":"Specifies preferences for transit routes. Using this parameter, you can bias the options returned, rather than accepting the default best route chosen by the API. This parameter may only be specified for transit directions, and only if the request includes an API key. The parameter supports the following arguments:<br /><i>less_walking</i> - indicates that the calculated route should prefer limited amounts of walking.<br /><i>fewer_transfers</i> - indicates that the calculated route should prefer a limited number of transfers.",
            "required":false,
            "type":"string"
         }
      ],
      "responses":{  
         "200":{  
            "description":"The directions request response as provided by Google.",
            "schema":{  
               "properties":{  
                  "status":{  
                     "type":"string"
                  },
                  "routes":{  
                     "type":"array",
                     "items":{  
                        "type":"string"
                     }
                  }
               }
            }
         },
         "400":{  
            "description":"Bad request. Please check additional message for more information.",
            "schema":{  
               "properties":{  
                  "status":{  
                     "type":"string"
                  },
                  "code":{  
                     "type":"integer"
                  },
                  "message":{  
                     "type":"string"
                  }
               }
            }
         },
         "401":{  
            "description":"You provided an API Key that is invalid, or is not authorized to use the Google Directions API.",
            "schema":{  
               "properties":{  
                  "status":{  
                     "type":"string"
                  },
                  "code":{  
                     "type":"integer"
                  },
                  "message":{  
                     "type":"string"
                  }
               }
            }
         },
         "429":{  
            "description":"You have used the daily allotment of Google Directions requests for the provided API Key.",
            "schema":{  
               "properties":{  
                  "status":{  
                     "type":"string"
                  },
                  "code":{  
                     "type":"integer"
                  },
                  "message":{  
                     "type":"string"
                  }
               }
            }
         },
         "500":{  
            "description":"An unknown error occurred. Please try your request again.",
            "schema":{  
               "properties":{  
                  "status":{  
                     "type":"string"
                  },
                  "code":{  
                     "type":"integer"
                  },
                  "message":{  
                     "type":"string"
                  }
               }
            }
         }
      },
      "deprecated":false
   },
   {  
      "id":"690425ec.96fbdc",
      "type":"catch",
      "name":"",
      "x":168,
      "y":239,
      "z":"ef8b8f86.10747",
      "wires":[  
         [  
            "7ca6bfe4.83594"
         ]
      ]
   },
   {  
      "id":"7ca6bfe4.83594",
      "type":"function",
      "name":"Move error to http-out",
      "func":"var error = msg._error;\nmsg.payload = error;\nif(error){\n    msg.statusCode = error.code || 500;\n} else{\n    msg.statusCode = 500;\n}\nreturn msg;",
      "outputs":1,
      "valid":true,
      "x":393,
      "y":239,
      "z":"ef8b8f86.10747",
      "wires":[  
         [  
            "103809e8.efc7f6"
         ]
      ]
   },
   {  
      "id":"103809e8.efc7f6",
      "type":"http response",
      "name":"",
      "x":583,
      "y":239,
      "z":"ef8b8f86.10747",
      "wires":[  

      ]
   },
   {  
      "id":"6a5bfc67.95a404",
      "type":"http in",
      "name":"",
      "url":"/directions/getRoutes",
      "method":"get",
      "swaggerDoc":"e4daec3b.1b251",
      "x":203.99998474121094,
      "y":94,
      "z":"ef8b8f86.10747",
      "wires":[  
         [  
            "792228be.86ddd8"
         ]
      ]
   },
   {  
      "id":"99907bd5.666f88",
      "type":"http response",
      "name":"",
      "x":888.5714111328125,
      "y":94.14285278320312,
      "z":"ef8b8f86.10747",
      "wires":[  

      ]
   },
   {  
      "id":"792228be.86ddd8",
      "type":"function",
      "name":"Move query params to msg",
      "func":"for(var key in msg.req.query){\n    msg[key] = msg.req.query[key];\n}\nreturn msg;",
      "outputs":1,
      "valid":true,
      "x":443.5714111328125,
      "y":94,
      "z":"ef8b8f86.10747",
      "wires":[  
         [  
            "82dbdbc9.7d2428"
         ]
      ]
   },
   {  
      "id":"82dbdbc9.7d2428",
      "type":"google directions",
      "name":"",
      "origin":"",
      "destination":"",
      "mode":"",
      "waypoints":"",
      "alternatives":"",
      "avoid":"",
      "language":"",
      "units":"",
      "region":"",
      "departure_time":"",
      "arrival_time":"",
      "transit_mode":"",
      "transit_routing_preferences":"",
      "x":700.5714111328125,
      "y":94,
      "z":"ef8b8f86.10747",
      "wires":[  
         [  
            "99907bd5.666f88"
         ]
      ]
   }
]
lostinthestory

Flow Info

created 1 year, 11 months ago

Node Types

Core
  • catch (x1)
  • function (x2)
  • http in (x1)
  • http response (x2)
Other

Tags

  • Google
  • Directions
  • GoogleDirections
  • GoogleMaps
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option