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