Skip to content

Bing GeoCodeService VB

Public Sub Main()
If Dts.Variables.Contains("Address") And Dts.Variables.Contains("Lat") And Dts.Variables.Contains("Long") Then
Try
' Set a Bing Maps key before making a request

Dim key As String = "[Get Key from Bing API page]"

Dim geocodeRequest As New bing.geocode.GeocodeRequest

Dim SearchAddress As String
SearchAddress = Dts.Variables("Address").Value.ToString
Dts.Events.FireInformation(0, "Address:", SearchAddress, "", 0, True)

' 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 = SearchAddress

' Set the options to only return high confidence results 
Dim filters As bing.geocode.ConfidenceFilter() = New bing.geocode.ConfidenceFilter(0) {}
filters(0) = New bing.geocode.ConfidenceFilter()
filters(0).MinimumConfidence = bing.geocode.Confidence.High

Dim geocodeOptions As New bing.geocode.GeocodeOptions()
geocodeOptions.Filters = filters

geocodeRequest.Options = geocodeOptions

' 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 AndAlso geocodeResponse.Results(0).Locations.Length > 0 Then
Dts.Events.FireInformation(0, "Lat:", geocodeResponse.Results(0).Locations(0).Latitude, "", 0, False)
Dts.Variables("Lat").Value = geocodeResponse.Results(0).Locations(0).Latitude
Dts.Events.FireInformation(0, "Long:", geocodeResponse.Results(0).Locations(0).Longitude, "", 0, True)
Dts.Variables("Long").Value = geocodeResponse.Results(0).Locations(0).Longitude
End If

Catch ex As Exception
Dts.Events.FireError(-1, "Error:", ex.Message, "", 0)
Dts.TaskResult = ScriptResults.Success
End Try
Else
Dts.Events.FireError(-1, "Error:", "Missing vairable in Task Component Definition.", "", 0)
End If

End Sub
Leave a Comment

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