Code: Form 1
The form 1 class starts out with the default class declaration. An import statement was added to pull in the System.Text library; this is required to support the use of string builders within the application.
Imports System.Text
Public Class Form1
Following the class declaration, the next bit of code in the class is used to handle the button click event that requests a map of a physical address from Google Maps; that code is as follows:
''' <summary>
''' Map a physical address
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnMapIt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMapIt.Click
Try
Dim street As String = String.Empty
Dim city As String = String.Empty
Dim state As String = String.Empty
Dim zip As String = String.Empty
Dim queryAddress As New StringBuilder()
queryAddress.Append("http://maps.google.com/maps?q=")
' build street part of query string
If txtStreet.Text <> String.Empty Then
street = txtStreet.Text.Replace(" ", "+")
queryAddress.Append(street + "," & "+")
End If
' build city part of query string
If txtCity.Text <> String.Empty Then
city = txtCity.Text.Replace(" ", "+")
queryAddress.Append(city + "," & "+")
End If
' build state part of query string
If txtState.Text <> String.Empty Then
state = txtState.Text.Replace(" ", "+")
queryAddress.Append(state + "," & "+")
End If
' build zip code part of query string
If txtZipCode.Text <> String.Empty Then
zip = txtZipCode.Text.ToString()
queryAddress.Append(zip)
End If
' pass the url with the query string to web browser control
webBrowser1.Navigate(queryAddress.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve Map")
End Try
End Sub
A quick examination of this code reveals that the contents of the text boxes used to define the physical address are captured from the form and used to populate variables local to the event handler. A string builder is used to build the URL and query string.
Once the string builder is initialized, the first part of the URL is appended to it. As each additional section of the address is conditioned for use in the query string, it too is appended to the query string. Once the string is defined, it is passed to the web browser control as its navigate URL. At that point, the web browser will display the address if it can be found or will summon Goggle Maps with optional selections used to narrow down to a single address.
The next bit of code in the application performs a similar function with Latitudes and Longitudes; the click event handler for the button is used to evoke the map based upon the user supplied lat/long values. That code is as follows:
''' <summary>
''' Map a location by latitude and longitude
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnMapLatLong_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMapLatLong.Click
If txtLat.Text = String.Empty Or txtLong.Text = String.Empty Then
MessageBox.Show("Supply a latitude and longitude value.", "Missing Data")
End If
Try
Dim lat As String = String.Empty
Dim lon As String = String.Empty
Dim queryAddress As New StringBuilder()
queryAddress.Append("http://maps.google.com/maps?q=")
' build latitude part of query string
If txtLat.Text <> String.Empty Then
lat = txtLat.Text
queryAddress.Append(lat + "%2C")
End If
' build longitude part of query string
If txtLong.Text <> String.Empty Then
lon = txtLong.Text
queryAddress.Append(lon)
End If
webBrowser1.Navigate(queryAddress.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error")
End Try
End Sub