''' <summary>
'''
Use the geocoder.us web service
'''
to geocode and address; display both
'''
the best matching address along
'''
with a collection of all addresses
'''
meeting the search criteria
''' </summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
''' <remarks></remarks>
Private Sub GetLatLong(ByVal sender As Object,
_ByVal e Asus.geocoder.rpc.geocode_addressCompletedEventArgs)
Try
'
the address is broken up into parts so
'
reassemble them into a street address using
'
only the parts that have been populated
'
by the service
'
start with the street number
txtStreet.Text
= e.Result(0).number.ToString() + "
"
'
add the prefix (e.g., N, SE, etc.)
If e.Result(0).prefix.ToString()
<> String.Empty Then
txtStreet.Text
+= e.Result(0).prefix.ToString() + "
"
End If
'
add the street name
If e.Result(0).street.ToString()
<> String.Empty Then
txtStreet.Text
+= e.Result(0).street.ToString() + "
"
End If
'
add the suffix (if any)
If e.Result(0).suffix.ToString()
<> String.Empty Then
txtStreet.Text
+= e.Result(0).suffix.ToString() + "
"
End If
'add
the street type (e.g., ST, CT, RD, PKWY, etc.)
If e.Result(0).type.ToString()
<> String.Empty Then
txtStreet.Text
+= e.Result(0).type.ToString()
End If
'
add the city, state, and zip
txtCity.Text
= e.Result(0).city.ToString()
txtState.Text
= e.Result(0).state.ToString()
txtZip.Text
= e.Result(0).zip.ToString()
'
display the latitude and longitude
txtLatitude.Text
= e.Result(0).lat.ToString()
txtLongitude.Text
= e.Result(0).long.ToString()
'
create a datatable to hold any additional
'
possible address matches
Dim dt As New DataTable()
'
define all of the columns and add them
'
to the data table
Dim dcAddress As New DataColumn()
dcAddress.DataType
= System.Type.GetType("System.String")
dcAddress.ColumnName
= "Address"
dcAddress.Caption
= "Address"
dt.Columns.Add(dcAddress)
Dim dcCity As New DataColumn()
dcCity.DataType
= System.Type.GetType("System.String")
dcCity.ColumnName
= "City"
dcCity.Caption
= "City"
dt.Columns.Add(dcCity)
Dim dcState As New DataColumn()
dcState.DataType
= System.Type.GetType("System.String")
dcState.ColumnName
= "State"
dcState.Caption
= "State"
dt.Columns.Add(dcState)
Dim dcZip As New DataColumn()
dcZip.DataType
= System.Type.GetType("System.String")
dcZip.ColumnName
= "Zip
Code"
dcZip.Caption
= "Zip"
dt.Columns.Add(dcZip)
Dim dcLat As New DataColumn()
dcLat.DataType
= System.Type.GetType("System.String")
dcLat.ColumnName
= "Latitude"
dcLat.Caption
= "Lat"
dt.Columns.Add(dcLat)
Dim dcLong As New DataColumn()
dcLong.DataType
= System.Type.GetType("System.String")
dcLong.ColumnName
= "Longitude"
dcLong.Caption
= "Lon"
dt.Columns.Add(dcLong)
'
create a row
Dim row As DataRow
'
loop through all of the results and
'
add each row to the data table for
'
display in the All Possible Matches
'
datagridview control
Dim i As Int32
For i
= 0 To e.Result.Length
- 1
'
create a new row
row
= dt.NewRow()
'
build a street address
Dim addr As String =
e.Result(i).number.ToString() & "
"
If e.Result(i).prefix.ToString()
<> String.Empty Then
addr
+= e.Result(0).prefix.ToString() + "
"
End If
If e.Result(i).street.ToString()
<> String.Empty Then
addr
+= e.Result(i).street.ToString() + "
"
End If
If e.Result(i).suffix.ToString()
<> String.Empty Then
addr
+= e.Result(i).suffix.ToString() + "
"
End If
If e.Result(i).type.ToString()
<> String.Empty Then
addr
+= e.Result(i).type.ToString()
End If
'
set the street address
row("Address")
= addr
'
set the city, state, and zip code
row("City")
= e.Result(i).city.ToString()
row("State")
= e.Result(i).state.ToString()
row("Zip
Code") = e.Result(i).zip.ToString()
'
set the latitude and longitude
row("Latitude")
= e.Result(i).lat.ToString()
row("Longitude")
= e.Result(i).long.ToString()
'
add the new row to the table
dt.Rows.Add(row)
Next
'
configure and bind the grid to the table
dgvMatches.DataSource
= dt
dgvMatches.SelectionMode
= _
DataGridViewSelectionMode.FullRowSelect
dgvMatches.AutoSizeRowsMode
= _
DataGridViewAutoSizeRowsMode.AllCells
dgvMatches.RowHeadersVisible
= False
dgvMatches.ScrollBars
= ScrollBars.Both
dgvMatches.Refresh()
Catch ex As Exception
MessageBox.Show(ex.Message, "Address")
End Try
''' <summary>
'''
Click event handler used to evoke
'''
the web service and to geocode the
'''
physical address passed to that
'''
service
''' </summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
''' <remarks></remarks>
Private Sub btnGeocode_Click(ByVal sender As System.Object,
_ByVal e As System.EventArgs)Handles btnGeocode.Click
'
make sure there is an address
'
to geocode
If txtSearchAddress.Text
= String.Empty Then
MessageBox.Show("Invalid
search address.", "Error")
Return
End If
Try
'
instance the geocoder service
Dim gcAddress As New us.geocoder.rpc.GeoCode_Service()
'
the event handler for completion of the
'
geocoding does the work of displaying the
'
results the site offers alternative
'
methods for viewing the geocoding results
AddHandler gcAddress.geocode_addressCompleted,
_AddressOf GetLatLong
'
call the asynchronous version of the geocoder
gcAddress.geocode_addressAsync(txtSearchAddress.Text)
'
dispose of the service instance
gcAddress.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Geocoding
Error")
End Try