在C#中动态传递多个纬度和经度给Google Distance Matrix API,您可以使用以下步骤:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
private static async Task<string> GetDistanceMatrixResponseAsync(string url)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
private static string BuildDistanceMatrixUrl(string apiKey, List<string> origins, List<string> destinations)
{
string url = "https://maps.googleapis.com/maps/api/distancematrix/json?";
url += $"key={apiKey}";
string originsString = string.Join("|", origins);
url += $"&origins={Uri.EscapeDataString(originsString)}";
string destinationsString = string.Join("|", destinations);
url += $"&destinations={Uri.EscapeDataString(destinationsString)}";
return url;
}
static async Task Main(string[] args)
{
string apiKey = "YOUR_API_KEY";
List<string> origins = new List<string> { "40.712776,-74.005974", "34.052235,-118.243683" };
List<string> destinations = new List<string> { "37.774929,-122.419416", "51.5074,-0.1278" };
string url = BuildDistanceMatrixUrl(apiKey, origins, destinations);
string response = await GetDistanceMatrixResponseAsync(url);
Console.WriteLine(response);
}
请注意,上述代码仅展示了如何在C#中动态传递多个纬度和经度给Google Distance Matrix API,并打印出API的响应。您可能需要根据自己的需求进一步处理API响应数据。
关于Google Distance Matrix API的更多信息,您可以访问腾讯云的相关产品介绍页面:腾讯云API网关。
领取专属 10元无门槛券
手把手带您无忧上云