[英]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=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++;
}
}
}
答案是這種類型的方法創建的查詢字符串太長,以至於請求中只有這么多個地址。 因此Google API工作正常,但是我的請求被最大查詢長度截斷了。 我通過使用提琴手並檢查請求值發現了這一點,發現其中只有10個地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.