Skip to content

Bing GeoCodeService C#

 
public void Main()
{
string Results = "";
Boolean varCheck = true;
varCheck = Dts.Variables.Contains("Address");
varCheck = Dts.Variables.Contains("Lat");
varCheck = Dts.Variables.Contains("Long");
if(varCheck == false)
{ return; }
try
{
// Set a Bing Maps key before making a request
string key = "Bing Maps Key";
bing.geocode.GeocodeRequest geocodeRequest = new bing.geocode.GeocodeRequest();
// Set the credentials using a valid Bing Maps Key
geocodeRequest.Credentials = new bing.geocode.Credentials();
geocodeRequest.Credentials.ApplicationId = key;
// Set the full address query
geocodeRequest.Query = "1 Microsoft Way, Redmond, WA";
// Set the options to only return high confidence results
bing.geocode.ConfidenceFilter[] filters = new bing.geocode.ConfidenceFilter[1];
filters[0] = new bing.geocode.ConfidenceFilter();
filters[0].MinimumConfidence =bing.geocode.Confidence.High;
bing.geocode.GeocodeOptions geocodeOptions = new bing.geocode.GeocodeOptions();
geocodeOptions.Filters = filters;
geocodeRequest.Options = geocodeOptions;
// Make the geocode request
bing.geocode.GeocodeService geocodeService = new bing.geocode.GeocodeService();
bing.geocode.GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);
Results = geocodeResponse.Results[0].DisplayName;
// Make the geocode request
//Dim geocodeService As New bing.geocode.GeocodeService
// Dim geocodeResponse As bing.geocode.GeocodeResponse = geocodeService.Geocode(geocodeRequest)
if(geocodeResponse.Results.Length > 0 && geocodeResponse.Results[0].Locations.Length > 0) 
{

Dts.Variables["Lat"].Value = geocodeResponse.Results[0].Locations[0].Latitude;

Dts.Variables["Long"].Value = geocodeResponse.Results[0].Locations[0].Longitude; 
};
}
catch (Exception ex)
{
Results = "An exception occurred: " + ex.Message;
}
Dts.TaskResult = (int)ScriptResults.Success;
}

 
3 Comments
  1. I tried this out but I get an error when I try to make the geocode address: “Could not find default endpoint element that references contract ‘BingGeocode.IGeocodeService’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.”

    I see the end points in my app.config

    • When I try to make the geocode request not address.
      bing.geocode.GeocodeService geocodeService = new bing.geocode.GeocodeService();

    • SQLMD permalink

      I have to give you the horrible helpdesk response that I am unable to recreate this so I am not sure what the issue might be. Are you doing this in SSIS or as part of a webservice?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 590 other followers