byteC0de
byteC0de

Reputation: 5273

How to get direction between 3 points, from maps.googleapis

How to get direction between 3 points, like from A to B to C. My current code is getting directions from 2 points.

private String getDirectionsUrl(LatLng origin, LatLng dest) {

        // Origin of route
        String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

        // Destination of route
        String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + sensor;

        // Output format
        String output = "json";

        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + getResources().getString(R.string.googleApiKey);

        return url;
    }

Upvotes: 1

Views: 264

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521229

I think the directions API only has a single origin and destination. But the API has a feature called waypoints, which allow you to route a trip through one or more other points. If your original trip were A -> C and you now want to add a stop at B, then you could add a waypoint, which would ensure that the trip passes through B.

From the documentation, here is a sample URL with two waypoints:

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

Upvotes: 1

Related Questions