简体   繁体   中英

Why is google distance matrix API only returning 10 results, when passed 52?

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++;
            }
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM