【发布时间】:2014-08-16 07:39:00
【问题描述】:
我正在寻找那些有使用 Bing API 批处理作业经验的人。我已经能够使用 Microsoft 的 Bing Spatial Data Services 文档中的示例代码批量处理地址并返回地理编码,但无法批量处理“路线”以获得两个地址或两组地理位置之间的行驶时间。以下是将持续时间作为单个查询获取的 http 查询,然后是用于批量查询的示例函数。
编辑: 我想通过在这个 sn-p 中将 'geocode' 交换为 'Routes' 将是一种直观的方式(如果它甚至在批处理模式下受支持):
//Build the HTTP URI that will upload and create the geocode dataflow job
UriBuilder uriBuilder = new UriBuilder("http://spatial.virtualearth.net");
uriBuilder.Path = "/REST/v1/dataflows/geocode";
uriBuilder.Query = queryStringBuilder.ToString();
感谢任何指针、示例、链接显示如何使用 Bing API 批处理路由。 (注意:需要 Bing 密钥才能在 URL 下运行。)
路由网址:
地址到地理编码的批处理作业:
static string CreateJob(string dataFilePath, string dataFormat, string key, string description)
{
string contentType = "text/plain";
if (dataFormat.Equals("xml", StringComparison.OrdinalIgnoreCase))
contentType = "application/xml";
StringBuilder queryStringBuilder = new StringBuilder();
queryStringBuilder.Append("input=").Append(Uri.EscapeUriString(dataFormat));
queryStringBuilder.Append("&");
queryStringBuilder.Append("key=").Append(Uri.EscapeUriString(key));
if (!String.IsNullOrEmpty(description))
{
queryStringBuilder.Append("&");
queryStringBuilder.Append("description=").Append(Uri.EscapeUriString(description));
}
//Build the HTTP URI that will upload and create the geocode dataflow job
UriBuilder uriBuilder = new UriBuilder("http://spatial.virtualearth.net");
uriBuilder.Path = "/REST/v1/dataflows/geocode";
uriBuilder.Query = queryStringBuilder.ToString();
//Include the data to geocode in the HTTP request
using (FileStream dataStream = File.OpenRead(dataFilePath))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
request.Method = "POST";
request.ContentType = contentType;
using (Stream requestStream = request.GetRequestStream())
{
byte[] buffer = new byte[16384];
int bytesRead = dataStream.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
requestStream.Write(buffer, 0, bytesRead);
bytesRead = dataStream.Read(buffer, 0, buffer.Length);
}
}
//Submit the HTTP request and check if the job was created successfully.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.Created)
throw new Exception("An HTTP error status code was encountered when creating the geocode job.");
string dataflowJobLocation = response.GetResponseHeader("Location");
if (String.IsNullOrEmpty(dataflowJobLocation))
throw new Exception("The 'Location' header is missing from the HTTP response when creating a goecode job.");
return dataflowJobLocation;
}
}
}
【问题讨论】:
-
有没有找到这样做的方法?我需要找到从邮政编码到英国大约 150 个地点的行车距离,才能找到最近的 5 个。这是在一个每天有数千次访问的网页中。所以它需要高效和快速。接受的答案听起来像是一个糟糕的妥协。
标签: c# bing-maps bing bing-api