简体   繁体   English

在C#中,如何通过经度和纬度值从“必应地图”中获取最近的位置?

[英]In C#, how do I get a nearest place from Bing Maps from a longitude and latitude value?

Yes, I'm very new to this, so I can't really understand all the documentation Bing has put up. 是的,我对此很陌生,所以我无法真正理解Bing提交的所有文档。 But if I have a double (numerical) value of longitude and latitude, then how can I get the nearest place, preferably as a string, in C#? 但是,如果我有一个经度和纬度的双(数值)值,那么如何在C#中获得最近的位置(最好是字符串形式)? I sort of knew how to do this in MSRMaps from the old tutorial, but I have absolutely no idea how to do this here. 我从旧教程中知道如何在MSRMaps中执行此操作,但是我绝对不知道如何在此处执行此操作。

Thank you very much! 非常感谢你!

EDIT: I found this tutorial here: http://msdn.microsoft.com/en-us/library/dd221354.aspx 编辑:我在这里找到了本教程: http : //msdn.microsoft.com/en-us/library/dd221354.aspx

And I decided to just go for the Reverse Geocode bit, and this is what I got: 我决定只选择反向地理编码,这就是我得到的:

static void Main(string[] args)
    {
        string location;
        location = ReverseGeocodePoint("47.608, -122.337");
        Console.WriteLine(location);

    }



    static string ReverseGeocodePoint(string locationString)
    {
        string results = "";
        string key = "Aq4VS_9C4juJKsP7hRFqWlYj0Mpd_ybl2vOmj_J9rugPvptWiOEa3tCzmXWvzm9J";
        ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

        // Set the credentials using a valid Bing Maps key
        reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
        reverseGeocodeRequest.Credentials.ApplicationId = key;

        // Set the point to use to find a matching address
        GeocodeService.Location point = new GeocodeService.Location();
        string[] digits = locationString.Split(',');

        point.Latitude = double.Parse(digits[0].Trim());
        point.Longitude = double.Parse(digits[1].Trim());

        reverseGeocodeRequest.Location = point;

        // Make the reverse geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient();
        GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

        if (geocodeResponse.Results.Length > 0)
        results = geocodeResponse.Results[0].DisplayName;
        else
        results = "No Results found";

        return results;
     }
 }

Only I got an error at GeocodeServiceClient geocodeService = new GeocodeServiceClient(); 只有我在GeocodeServiceClient geocodeService = new GeocodeServiceClient();处出错。

saying: An endpoint configuration section for contract 'GeocodeService.IGeocodeService' could not be loaded because more than one endpoint configuration for that contract was found. 说法:无法加载合同'GeocodeService.IGeocodeService'的端点配置部分,因为找到了该合同的多个端点配置。 Please indicate the preferred endpoint configuration section by name. 请按名称指示首选端点配置部分。

What does this mean for me? 这对我意味着什么?

Try starting here. 尝试从这里开始。 Geocode Service In particular what you're looking for is Reverse Geocoding 地理编码服务特别是您要寻找的是反向地理编码

As per example on MSDN. 按照MSDN上的示例。

// Set a Bing Maps key before making a request
string key = "Bing Maps Key";

GeocodeService.ReverseGeocodeRequest reverseGeocodeRequest = new GeocodeService.ReverseGeocodeRequest();

// Set the credentials using a valid Bing Maps key
reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
reverseGeocodeRequest.Credentials.ApplicationId = key;

// Set the point to use to find a matching address
GeocodeService.Location point = new GeocodeService.Location();
point.Latitude = 47.608;
point.Longitude = -122.337;

reverseGeocodeRequest.Location = point;

// Make the reverse geocode request
GeocodeService.GeocodeServiceClient geocodeService =
new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
GeocodeService.GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

Results = geocodeResponse.Results[0].DisplayName;

You have to delete One of the endpoints from your App.Config/web.config 您必须从App.Config / web.config中删除其中一个端点

Original Generated 原始生成

<client>
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
                contract="Microsoft.Bing.GeocodeService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
                binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService"
                contract="Microsoft.Bing.GeocodeService.IGeocodeService" name="CustomBinding_IGeocodeService" />
        </client>

If you remove one of the endpoint configurations from your app.config/web.config your code will just work fine. 如果您从app.config / web.config中删除端点配置之一,您的代码将可以正常工作。

 <client>
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
                contract="Microsoft.Bing.GeocodeService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
        </client>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM