Bing API - 驾驶持续时间和距离的批量作业

时间:2014-08-16 07:39:01

标签: c# bing-maps bing bing-api

我正在寻找具有使用Bing API批处工作经验的人。我已经能够使用Microsoft Bing Spatial Data Services文档中的示例代码批处理地址并返回地理编码,但是无法批处理“路由”以便在两个地址或两组地理位置之间获得行驶持续时间。下面是http查询,用于将持续时间作为单个查询,然后是样本函数到批量查询。

编辑: 我想通过在这个片段中交换'Routes'的'geocode'将是一种直观的方式(如果它甚至在批处理模式下支持):

    //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批量路由的任何指针,示例和链接。 (注意:要在URL下面运行,需要使用Bing键。)

路线网址:

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;
            }
        }
}

1 个答案:

答案 0 :(得分:1)

Bing Maps没有用于获取路由距离/时间的批处理服务,但是,您可以使用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