简体   繁体   中英

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. 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#? 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.

Thank you very much!

EDIT: I found this tutorial here: 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();

saying: An endpoint configuration section for contract 'GeocodeService.IGeocodeService' could not be loaded because more than one endpoint configuration for that contract was found. 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.

// 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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