繁体   English   中英

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

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

是的,我对此很陌生,所以我无法真正理解Bing提交的所有文档。 但是,如果我有一个经度和纬度的双(数值)值,那么如何在C#中获得最近的位置(最好是字符串形式)? 我从旧教程中知道如何在MSRMaps中执行此操作,但是我绝对不知道如何在此处执行此操作。

非常感谢你!

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

我决定只选择反向地理编码,这就是我得到的:

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

只有我在GeocodeServiceClient geocodeService = new GeocodeServiceClient();处出错。

说法:无法加载合同'GeocodeService.IGeocodeService'的端点配置部分,因为找到了该合同的多个端点配置。 请按名称指示首选端点配置部分。

这对我意味着什么?

尝试从这里开始。 地理编码服务特别是您要寻找的是反向地理编码

按照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;

您必须从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" />
            <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>

如果您从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