I am trying to get the distance for various addresses from a source address and am able to call the google distance matrix API, but only 10 results are returned instead of a result for each address sent. I am using restsharp library in c#. My understanding from the documentation is that it should work for up to 100 addresses at a time.
private void CalculateDistance(List<StoreViewModel> tempList, string lattitude, string longitude)
{
var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&key=removedForStackOverflowPostInsertYourKeyHere&origins=";
string destinations = string.Empty;
foreach(StoreViewModel vm in tempList)
{
destinations += "|";
destinations += vm.AddressLine1;
destinations += string.IsNullOrWhiteSpace(vm.AddressLine2) ? "" : "+" + vm.AddressLine2;
destinations += string.IsNullOrWhiteSpace(vm.AddressLine3) ? "" : "+" + vm.AddressLine3;
destinations += "+" + vm.City;
destinations += "+" + vm.State;
destinations += "+" + vm.ZIP;
destinations += "|";
}
url += lattitude + "," + longitude;
url += "&destinations=";
url += destinations;
var client = new RestClient(url);
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json; charset=utf-8");
var response = client.Execute(request);
var jsonObject = System.Web.Helpers.Json.Decode(response.Content);
int i = 0;
foreach(var row in jsonObject.rows)
{
foreach(var element in row.elements)
{
if(element!=null && element.distance!=null)
{
tempList[i].DistanceFromUser = element.distance.value;
tempList[i].DistanceFromUserText = element.distance.text;
}
else
{
tempList[i].DistanceFromUser = 99999999;
tempList[i].DistanceFromUserText = "Distance not available.";
}
i++;
}
}
}