【问题标题】:Bing API - Batch job of driving durations and distancesBing API - 驾驶时间和距离的批处理作业
【发布时间】: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 下运行。)

路由网址:

http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=Minneapolis,MN&wp.1=St%20Paul,MN&optmz=distance&rpo=Points&key=BingMapsKey

地址到地理编码的批处理作业:

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


【解决方案1】:

Bing 地图没有用于获取路由距离/时间的批处理服务,但是,您可以使用 REST 路由服务进行多次调用来执行此操作。您可以进行的一项优化以加快速度是在单个请求中使用多个航点。 Bing Maps 在一个请求中最多允许 25 个航点。例如,如果您想获取从 A 到 B 和 A 到 C 的路线距离,您可以创建一个从 A 到 B 到 A 到 C 的路线请求,然后在响应中循环通过路线支路。所有索引为偶数的路线航段都是从 A 到目的地。然后,您可以抓住那条腿的距离和时间。

您需要使用此服务:http://msdn.microsoft.com/en-us/library/ff701705.aspx

您可以在此处找到有关如何在 .NET 中使用此服务的信息: http://msdn.microsoft.com/en-us/library/jj819168.aspx

http://msdn.microsoft.com/en-us/library/jj870778.aspx

【讨论】:

  • 我很害怕,因为我在 Dataflows 文档中找不到任何提及“路由”的内容。关于使用航点的有趣建议。谢谢!
猜你喜欢
  • 2012-01-18
  • 2020-06-10
  • 2012-12-26
  • 2013-05-22
  • 2012-08-31
  • 2013-06-21
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多