简体   繁体   English

为什么Google距离矩阵API传递52时仅返回10个结果?

[英]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. 我正在尝试从源地址获取各种地址的距离,并且能够调用Google距离矩阵API,但是仅返回10个结果,而不是每个发送地址的结果。 I am using restsharp library in c#. 我在C#中使用restsharp库。 My understanding from the documentation is that it should work for up to 100 addresses at a time. 我从文档中了解到,它一次最多可以处理100个地址。

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. 因此Google API工作正常,但是我的请求被最大查询长度截断了。 I discovered this by using fiddler and examining the request value and behold it had only 10 addresses in it. 我通过使用提琴手并检查请求值发现了这一点,发现其中只有10个地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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