使用多个目的地纬度/经度点的DistanceMatrixApi



我使用的客户端api规范代码是从位于此处的openapi规范创建的https://github.com/googlemaps/openapi-specification/releases.(v1.17.6版本(我创建了所有的客户端库,并使用它们来调用方法方法签名

我只使用前两个参数目的地和起点。

我有两个目的地和一个始发地。获取请求原来是这样的https://maps.googleapis.com/maps/api/distancematrix/json?destinations=42.7101%2c-78.8026%2c42.991077%2c-78.759279&起源=43.04179%2c-78.751965&key=[mykey]

答案是进入浏览器视图

我不知道发生了什么事。

发现问题,开放api规范中存在一个错误,该错误忽略了DistanceMatrixAsyncWithHttpInfo((方法中用于源和目的地的分隔符,该方法调用代码行

if (destinations != null) localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("pipe", "destinations", destinations)); // query parameter
if (origins != null) localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("pipe", "origins", origins)); // query parameter

ParameterToKeyValuePairs((方法没有传递";管道;到ParameterToString((。所以我不得不添加集合Format

public IEnumerable<KeyValuePair<string, string>> ParameterToKeyValuePairs(string collectionFormat, string name, object value)
{
var parameters = new List<KeyValuePair<string, string>>();
if (IsCollection(value) && collectionFormat == "multi")
{
var valueCollection = value as IEnumerable;
parameters.AddRange(from object item in valueCollection select new KeyValuePair<string, string>(name, ParameterToString(collectionFormat,item)));
}
else
{
parameters.Add(new KeyValuePair<string, string>(name, ParameterToString(collectionFormat, value)));
}
return parameters;
}

因此,在ParameterToString((方法中需要添加切换语句

else if (obj is IList)
{
var flattenedString = new StringBuilder();
foreach (var param in (IList)obj)
{
if (flattenedString.Length > 0)
switch (collectionFormat)
{
case "pipe": 
flattenedString.Append("|"); 
break;
default: 
flattenedString.Append(",");
break;
};
flattenedString.Append(param);
}
return flattenedString.ToString();
}

最新更新