google maps - Calculate optimized distance between a source and destination with multiple waypoints in between in c# -


i have fixed source , destination. have multiple waypoints in between. have calculate distance of optimized route between source , destination. have in c#.

thanks in advance.

you utilize google directions api purpose:

the google directions api service calculates directions between locations using http request

using waypoints in routes

when calculating routes using directions api, may specify waypoints driving, walking or bicycling directions.

for example, following url initiates directions request route between boston, ma , concord, ma stopovers in charlestown , lexington, in order:

https://maps.googleapis.com/maps/api/directions/json?origin=boston,ma&destination=concord,ma&waypoints=charlestown,ma|lexington,ma&key=api_key 

how consume google directions api in .net

the following example demonstrates how construct directions request:

public class directionsclient {     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 valid result             if ((string) data["status"] != "ok")             throw new exception("invalid request");             return data;         }        } } 

example

when directions api returns results, places them within (json) routes array.

the example shows how print legs info:

var data = directionsclient.calcroute("boston,ma", "concord,ma", new[] { "charlestown,ma", "lexington,ma"});  foreach (var route in data["routes"]) {     foreach (var leg in route["legs"])     {         console.writeline("start: {0} end {1}", leg["start_address"], leg["end_address"]);     }     } 

update

regarding format of request:

directions may specify origins, destinations , waypoints either text strings (e.g. "chicago, il" or "darwin, nt, australia") or latitude/longitude coordinates.

regarding route optimization:

by default, directions service calculates route through provided waypoints in given order. optionally, may pass optimize:true first argument within waypoints parameter allow directions service optimize provided route rearranging waypoints in more efficient order. (this optimization application of travelling salesman problem.)

if instruct directions service optimize order of waypoints, order returned in waypoint_order field within routes object. waypoint_order field returns values zero-based.

the following example calculates road trip route adelaide, south australia each of south australia's main wine regions using route optimization.

https://maps.googleapis.com/maps/api/directions/json?origin=adelaide,sa&destination=adelaide,sa&waypoints=optimize:true|barossa+valley,sa|clare,sa|connawarra,sa|mclaren+vale,sa&key=api_key 

inspection of calculated route indicate route calculated using following waypoint order:

"waypoint_order": [ 1, 0, 2, 3 ] 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -