2

I was using google directions api to get the route between two latlng points and plotting it on a google map. The issue is that i can optimize the route when adding waypoints but i can't control the optimization criteria. The code i am using for creating the request url is :

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";

    // Waypoints
    String waypoints = "";
    for (int i = 0; i < markerPoints.size(); i++) {

        LatLng point = (LatLng) markerPoints.get(i);
        if (i == 0)
            waypoints = "waypoints=optimize:true|";
        if (i == markerPoints.size() - 1) {
            waypoints += point.latitude + "," + point.longitude;
        } else {
            waypoints += point.latitude + "," + point.longitude + "|";
        }
    }

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + waypoints + "&mode=driving&key=YOUR_API_KEY";


    // Output format
    String output = "json";

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

    return url;
}

In short, i want to add optimization criteria in this string. Is it possible or do i have to enable alternate routes and manually calculate the travel distance of each path and choose the shortest?

betofarina
  • 1,066
  • 7
  • 9
Maaz Asif
  • 77
  • 6
  • Yes, you can and it seems you are doing it correctly, what is exactly the issue with your code? – betofarina Feb 09 '18 at 08:18
  • and you can get rid off the sensor paramater since is deprecated, as stated in the documentation: "The Google Maps API previously required that you include the sensor parameter to indicate whether your application used a sensor to determine the user's location. This parameter is no longer required." – betofarina Feb 09 '18 at 08:23
  • the optimization is being done on time not on distance and i want optimization of the whole route not just the ordering of the way-points. I read in the documentation that it can be done but i cant find how – Maaz Asif Feb 17 '18 at 15:54
  • I dont think there is a way to control the optimisation strategy used - its on or off from what I can understand, and Google decide what the best route ("Travel time is the primary factor which is optimized, but other factors such as distance, number of turns and many more may be taken into account when deciding which route is the most efficient." - https://developers.google.com/maps/documentation/directions/intro) – matt1 Feb 20 '18 at 14:47

0 Answers0