如何尝试使用Bing map rest APi进行地理编码。发布方式

时间:2018-12-24 06:54:51

标签: c# rest bing-maps

我提到了这个:https://docs.microsoft.com/en-us/bingmaps/rest-services/using-the-rest-services-with-net,并编写了以下代码。

             public void Geocode(string BingMapKey,string addressToBeGeocoded)
             {
                  Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?key={0}", BingMapKey));
                            GetPOSTResponse(geocodeRequest,addressToBeGeocoded, (x) =>
                            {
                                if (!(x.ResourceSets[0].Resources.Length > 0))// atleast one matching result has come.0 is the best matching result
                                {
                                    geocodeNotFound=true;
                                }
                            else
                                {

                                    BingMapsRESTService.Common.JSON.Location val = x.ResourceSets[0].Resources[0] as BingMapsRESTService.Common.JSON.Location;
                                    var latitude = val.GeocodePoints[0].Coordinates[0];
                                    var longitude = val.GeocodePoints[0].Coordinates[1];
                                }
                            }
                }

            private void GetPOSTResponse(Uri uri, string data, Action<BingMapsRESTService.Common.JSON.Response> callback)
            {
                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

                        request.Method = "POST";
                        request.ContentType = "text/plain;charset=utf-8";

                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        byte[] bytes = encoding.GetBytes(data);

                        request.ContentLength = bytes.Length;

                        using (Stream requestStream = request.GetRequestStream())
                        {
                            // Send the data.  
                            requestStream.Write(bytes, 0, bytes.Length);
                        }

                        request.BeginGetResponse((x) =>
                        {
                            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(x))
                            {
                                if (callback != null)
                                {
                                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(BingMapsRESTService.Common.JSON.Response));
                                    try
                                    {
                                        callback(ser.ReadObject(response.GetResponseStream()) as BingMapsRESTService.Common.JSON.Response);
                                    }

                                    catch (Exception e)
                                    {
                                        errorCount++;
                                    }
                            }
                            }
                        }, null);
            }

但是它给了我错误:             {"The remote server returned an error: (405) Method Not Allowed."}

如果我使用bing实例中提到的GET调用,它将起作用。我要使用通话功能。

我不知道Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations/key={0}", bingMapKey));应该是什么样子。我认为地址将在正文中。我错了吗?

1 个答案:

答案 0 :(得分:0)

Bing Maps中的Location API仅支持GET,不支持POST。这曾经在每个服务文档页面的顶部进行了记录,但是我不再看到它了。在过去的几周中,Bing Maps团队似乎将其文档从MSDN迁移到了docs.microsoft.com。

您可能还想考虑使用Microsoft较新的企业映射平台。 Azure地图。它具有更多功能,具有更高的免费使用限制,通常比免费限制便宜,与Bing Maps相比更定期地接收数据更新,并且是Azure云服务。对于简单的单个地理编码请求,它也仅支持GET请求(大多数映射平台都是这样),但对于更高级的搜索查询,则支持POST请求。您可以在Azure地图中找到类似的搜索服务:https://docs.microsoft.com/en-us/rest/api/maps/search

以下是Azure Maps上的一些其他资源: