为什么 google 距离矩阵 API 在传递 52 时只返回 10 个结果?
Why is google distance matrix API only returning 10 results, when passed 52?
我正在尝试从源地址获取各种地址的距离,并且能够调用 google 距离矩阵 API,但只返回 10 个结果,而不是每个结果地址发送。我在 C# 中使用 restsharp 库。
我从文档中了解到它一次最多可以处理 100 个地址。
private void CalculateDistance(List<StoreViewModel> tempList, string lattitude, string longitude)
{
var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&key=removedForWhosebugPostInsertYourKeyHere&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++;
}
}
}
答案是这种方法创建的查询字符串太长,请求中只有这么多地址。所以 Google API 工作正常,但我的请求被最大查询长度截断了。我通过使用 fiddler 并检查请求值发现了这一点,发现它只有 10 个地址。
我正在尝试从源地址获取各种地址的距离,并且能够调用 google 距离矩阵 API,但只返回 10 个结果,而不是每个结果地址发送。我在 C# 中使用 restsharp 库。 我从文档中了解到它一次最多可以处理 100 个地址。
private void CalculateDistance(List<StoreViewModel> tempList, string lattitude, string longitude)
{
var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&key=removedForWhosebugPostInsertYourKeyHere&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++;
}
}
}
答案是这种方法创建的查询字符串太长,请求中只有这么多地址。所以 Google API 工作正常,但我的请求被最大查询长度截断了。我通过使用 fiddler 并检查请求值发现了这一点,发现它只有 10 个地址。