0

So, this will be pretty straight forward question. I have function in helper class to fetch optimized (or not) route using maps API.

    public static JObject CalcRoute(string origin, string destination, string[] waypoints)
    {
        var requestUrl = string.Format("https://maps.googleapis.com/maps/api/directions/json?origin={0}&destination={1}&waypoints={2}", origin, destination, string.Join("|", waypoints));
        using (var client = new WebClient())
        {
            var result = client.DownloadString(requestUrl);
            var data = JsonConvert.DeserializeObject<JObject>(result);
            //ensure directions response contains a valid result
            string status = (string) data["status"];
            if ((string)data["status"] != "OK")
                throw new Exception("Invalid request");

            return data;
        }
    } 

And in my controller I am calling it like so:

        var data = GoogleGeocodeExtension.CalcRoute("startLat,endLat", "endLat,endLong", new[]
        {
            "optimize:true",
            "lat,lang",
            "lat,lang",
            //6 more lat and lang pairs
            //I want to optimise more than 10 limit
        });
            //showing data in debug window like so
            //so you can test it faster
        foreach (var route in data["routes"])
        {
            Debug.WriteLine("----------------------------------------------------");
            Debug.WriteLine(route["waypoint_order"]);
            Debug.WriteLine("----------------------------------------------------");
            foreach (var leg in route["legs"])
            {
                Debug.WriteLine("===================================================================================================");
                Debug.WriteLine("Start: {0} End: {1} \n Distance: {2} Duration: {3}", leg["start_address"], leg["end_address"], leg["distance"], leg["duration"]);
                Debug.WriteLine("Start lat lang: {0}", leg["start_location"]);
                Debug.WriteLine("End lat lang: {0}", leg["end_location"]);
                Debug.WriteLine("===================================================================================================");
            }
        }

So I can send 10 coordinates (lat & lang pairs), 2 as start and end position, and other 8 as way-points, but how to send 20? Or 30?

I have read many many question here on SO and other sites that mostly answered question about showing or calculating an already optimized list of coordinates.

I know I can can divide my list into multiple lists that have less than 10 coordinates, but in that way I wouldn't get properly optimized route, since it doesn't take into consideration all coordinates and only parts of it would properly optimized.

Unfortunately I can't afford to pay for premium licence to google (if I am not mistaken, it 10k freaking bucks :S).

EDIT: Apparently when using web directions service on server side, you can do that 23 way-points limit. What you need tho is that you add your key to API call like so:

var requestUrl = string.Format("https://maps.googleapis.com/maps/api/directions/json?key={3}&origin={0}&destination={1}&waypoints={2}", origin, destination, string.Join("|", waypoints), "YourApiKey");
Kadaj
  • 615
  • 3
  • 13
  • 31
  • The web directions service (which is what you are using) supports up to 23 waypoints in each server-side request for users of the standard API (you don't need a premium license). – geocodezip Sep 27 '16 at 16:24
  • Every other question I saw while I was looking for answer had one of yours answers xD Anyways, I don't know why you down-voted me but what you said is not true. When I try to add more than 8 way-points (as it is without key) I get status "MAX_WAYPOINTS_EXCEEDED", and when I add key to my call like so: var requestUrl = string.Format("https://maps.googleapis.com/maps/api/directions/json?origin={0}&destination={1}&waypoints={2}&key={3}", origin, destination, string.Join("|", waypoints),ApiKey); I get message "REQUEST_DENIED". So what am I doing wrong? – Kadaj Sep 28 '16 at 07:04
  • And to be fair you were right. I saw few examples where they put key in the end of request string, but it worked when i switched it to be first param like so: var requestUrl = string.Format("maps.googleapis.com/maps/api/directions/json?‌​​key={3}&origin={0}&destinati‌​on={1}&waypoints={2}‌", origin, destination, string.Join("|", waypoints),ApiKey); So it works now :D Thank you once again , even tho you hate me :D – Kadaj Sep 28 '16 at 07:20
  • Can you put that as answer so I can accept it? – Kadaj Sep 30 '16 at 13:22

1 Answers1

0

The web directions service (which is what you are using) supports up to 23 waypoints in each server-side request for users of the standard API (you don't need a premium license).

You do need a key, but keys are now (as of June 22, 2016) required for all of Google's mapping services.

geocodezip
  • 158,664
  • 13
  • 220
  • 245