Skip to content

API: Distance Matrix

EvaldasBend edited this page Mar 6, 2016 · 4 revisions

The Google Maps Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations. The information returned is based on the recommended route between start and end points, as calculated by the Google Maps API, and consists of rows containing duration and distance values for each pair.

Example

Let's assume we want to find distance between two points:
Batilly49.171020,5.969358
Aachen50.775346,6.083887

First we need list of origins and destinations:

Waypoint startPoint = new Waypoint(49.171020M, 5.969358M);
Waypoint endPoint = new Waypoint(54.555393M, 23.362615M);

SortedList<int, Waypoint> origin = new SortedList<int, Waypoint>();
SortedList<int, Waypoint> destination = new SortedList<int, Waypoint>();

origin.Add(0, startPoint);
destination.Add(0, endPoint);

After that we need to create request:

DistanceMatrixRequest request = new DistanceMatrixRequest()
{
     WaypointsOrigin = origin,
     WaypointsDestination = destination,
     Sensor = false
};

Then trigger query:

DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

Dispaly results:

Console.WriteLine(response.Rows.First().Elements.First().distance.Text);
Console.WriteLine(response.Rows.First().Elements.First().duration.Text);

Output:
1,677 km
16 hours 19 mins

Clone this wiki locally