0
votes

I am trying to prepare a following URL from java using com.google.maps google library. I am not finding proper library methods to add waypoint. Please let me know how to add waypoints to google map api.

https://maps.googleapis.com/maps/api/directions/json?origin=17.4366668,78.3982614&destination=17.42955,78.34171&waypoints=via: 17.44027,78.39431|via:17.43149,78.38817&key=AIzaSyDhhwfZgJv4DCVuX-RDuXLXfoHWL6FIPAw

I am following below approach to add origins and destinations to GeoAPi context.

GeoApiContext context = new GeoApiContext();
    context.setApiKey("AIzaSyDhhwfZgJv4DCVuX-RDuXLXfoHWL6FIPAw");

    LatLng originLatLng = new LatLng(17.4366668,78.3982614);

    LatLng destinationLatLng = new LatLng(17.42955,78.34171);

    LatLng wayPoints = new LatLng(17.4477, 78.38264);


    DirectionsResult result = DirectionsApi.newRequest(context)
            .origin(originLatLng)
            .destination(destinationLatLng)
            .waypoints("17.44027,78.39431", "17.43149,78.38817")
            .await();
2

2 Answers

0
votes

You can easily find the method for waypoints in source code on Github:

https://github.com/googlemaps/google-maps-services-java/blob/master/src/main/java/com/google/maps/DirectionsApiRequest.java

Starting from line 151:

/**
* Specifies a list 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.
*
* <p>For more information on waypoints, see <a href="https://developers.google.com/maps/documentation/directions/#Waypoints">
* Using Waypoints in Routes</a>.
*/
public DirectionsApiRequest waypoints(String... waypoints) {
  if (waypoints == null || waypoints.length == 0) {
    return this;
  } else if (waypoints.length == 1) {
    return param("waypoints", waypoints[0]);
  } else {
    return param("waypoints", (optimizeWaypoints ? "optimize:true|" : "") + join('|', waypoints));
  }
}
0
votes

The waypoints params could be pass with LatLng. Here is sample of code that working for me:

DirectionsResult result = DirectionsApi.newRequest(context)
                .mode(TravelMode.DRIVING)
                .origin(new LatLng(-7.372732, 110.50824))
                .waypoints(new LatLng(-7.272732, 110.508244), new LatLng(-7.172732, 110.508244))
                .optimizeWaypoints(true)
                .destination(new LatLng(-7.372732, 110.508244))
                .awaitIgnoreError();