0

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++;
            }
        }
    }
Chris
  • 968
  • 16
  • 27
  • 1
    I believe I read somewhere that the first 10 are free and if you want more you have to pay. – Terry Tyson May 22 '19 at 22:07
  • @TerryTyson Thank you for that! I think though, I have the API enabled, so it should just bill me instead of skipping some of the response data. I do see something about premium billing on the google site, so perhaps there is additional configuration needed to enable what I want, but if so its not obvious. I will keep digging. – Chris May 22 '19 at 23:44
  • I have opened a support case with google, so if its something they resolve, then I will update this here as well. – Chris May 22 '19 at 23:51

1 Answers1

1

The answer is that the query string created by this type of method is so long that only so many addresses where in the request. So the Google API was working fine, but my request was being truncated by max query length. I discovered this by using fiddler and examining the request value and behold it had only 10 addresses in it.

Chris
  • 968
  • 16
  • 27