While calculating routes using Google Direction API, we may specifying waypoint for driving, walking or bicycling directions. Though, Waypoints are not available for transit direction.

Parameters usage for Waypoint API:

We can pass one or more locations separated by pipe character (|), in form of address, location co-ordinate or place id of a location.

  • If you pass an address, the Direction service will geocode the address string and convert it into a assumpted latitude/longitude to calculate direction. This address may be different from that returned by the Google  Map Geocoding API.
  • If you pass specific or known latitude/longitude, they will used unchanged to calculate directions. But we need to make sure that, there is no space between latitude/longitude.
  • If you pass any specific place ID, we must need to pass place_id: key. This will only works when we pass and API key or Google Maps APIs Premium Plan client ID. PlaceID will be retrieved from Google Maps Geocoding API and Google Place API which includes Place Autocomplete.

Another way to use Waypoints is, passing an encoded set of coordinate using the Encoded Polyline Algorithm. This can be used when we have large numbers of waypoints in request. Encoded Polyline Algorithm makes request url significantly shorter than original request url.

  • Encoded Polylines must be prefixed with enc: and followed by colon(:). I.e.
  • waypoints=enc:fgo}EtohhU:

You can also include multiple encoded polylines, separated by pipe character(|). I.e. waypoints=enc:fgo}EtohhU:|enc:sdsd~sadsdsdhg:|enc:L}asdasdskhfjsdfjhf:

Google Direction API with Waypoints:

  • The following URL initiates a Direction request for a route between Baston, MA and Concord, MA with stopover Charlestown and Lexington, in that order:

https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&key=YOUR_API_KEY

  • For each in request direction, in response it will included an additional entry in the legs array which provide details for that leg of journey.
  • If you would like to get route using waypoints without adding a stopover, just add prefix via:. Waypoint prefix with via: will not add an entry to legs array, but it will instead route the journey through the provided waypoint.
  • The following URL will returned Waypoint without stopping at Laxington.

https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|via:Lexington,MA&key=YOUR_API_KEY

  • The via: prefix is most effective when creating routes in response to user dragging the waypoints on the map.
  • Using via: prefix to avoid stopovers results in directions that are very strict in their interpretation of the waypoint. This may lead result into ZERO_RESULTS status code if Google Map API is unable to create direction through that point.

Read more about using Google Maps API for Android app development.

  • The following URL requests waypoints using latitude/longitude:

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=via:-37.81223%2C144.96254%7Cvia:-34.92788%2C138.60008&key=YOUR_API_KEY

  • The following URL with encoded polyline:

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=via:enc:lexeF{~wsZejrPjtye@:&key=YOUR_API_KEY

Optimizing Waypoints:

  • By defaults, the Direction service calculates routes through the provided waypoints in their given order.
  • Optionally, you may pass optimize:true as the first argument within the waypoints parameter to allow the Direction service to optimize the provided route by rearranging the waypoints in a more efficient order.
  • If you instruct the Directions service to optimize the order of its waypoints, their order will be returned in the waypoint_order field within the routes object. The waypoint_order field returns values which are zero-based.
  • The following URL calculate routes from Adelaide, South Australia to each of south Australia’s main wine regions using route optimization:

https://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&key=YOUR_API_KEY

  • Inspection of the calculated route will indicate that the route is calculated using following waypoint order:

“waypoint_order”: [ 1, 0, 2, 3 ]

Restrictions:

  • Direction may be calculating using some restrictions. Restriction are indicated by use of the avoid parameter, and argument to that parameter indicating the restriction to avoid. Below are supporting parameters:

                     avoid=tolls

                     avoid=highways

                     avoid=ferries

  • It is also possible to pass any combination of these parameter in request.

avoid=tolls|highways|ferries

Write to us for robust API development at info@letsnurture.com for further queries. You can also share your thoughts with us by leaving us a tweet here – @letsnurture.

Want to work with us? We're hiring!