I need help with my code.
First, is my approach correct that I use the GoogleDistanceMatrix to get the real route distance and not just the linear distance?
I want to use an Geocoordinate for the origin and destination Adress but it doesnt seem to work:
public double CalculateGoogleDistance(GeoCoordinate from, GeoCoordinate to) {
DistanceMatrixRequest request = new DistanceMatrixRequest();
request.Key = "****";
request.Origins = new Location[] {
new Location(from)
};
request.Destinations = new Location[] {
new Location(to)
};
var response = GoogleApi.GoogleMaps.DistanceMatrix.Query(request);
return response.Rows.First().Elements.First().Distance.Value;
}
First off, you appear to be using 3rd party libraries/repositories in using Google Maps API services, so I would advice that you only follow their official documentations, in which I will be providing as I answer your questions.
As mentioned in their official documentation, even though it is optional, the default trafic_model
is bestguess
, meaning that the result will be based on the best estimate of travel time given what is known about both historical traffic conditions and live traffic. You can learn more about the other traffic model optional parameters here.
Now, with regards to getting "real route distance", you are only referring to the live result excluding historical traffic. To get this, you would need to specify the departureTime
to now as given in the example from the doc:
drivingOptions: {
departureTime: new Date(Date.now() + N), // for the time N milliseconds from now.
trafficModel: 'bestguess'
}
As you can see, that is how Distance Matrix API works.
Note: The departure time and traffic model is also applicable to Directions API.
See this link for the web service version of Distance Matrix API web service.