|
|
Title | Display a map for an address on Google maps or Yahoo maps in the system's default browser in Visual Basic 2005 |
Description | This example shows how to display a map for an address on Google maps or Yahoo maps in the system's default browser in Visual Basic 2005. |
Keywords | map, address lookup, Google, Yahoo, default browser, VB.NET, Visual Basic 2005 |
Categories | Internet |
|
|
When the user clicks a button, read the address from a text box and build an appropriate URL to load the map. Then use Process.Start to launch the URL in the system's default browser.
The following code shows how the program launches Google maps. It uses HttpUtility.UrlEncode to encode the address (convert spaces and other special characters so they don't confuse the browser) and inserts the encoded address into the basic map URL. It then checks a combo box to see if it should display a normal, satellite, or terrain map and makes the appropriate substitution. Finally it launches the URL.
|
|
Private Sub btnGoogle_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGoogle.Click
' The basic map URL without the address information.
Const URL_BASE As String = _
"http://maps.google.com/maps?f=q&hl=en&geocode=&time=&date=&ttype=&q=@ADDR@&ie=UTF8&t=@TYPE@"
' URL encode the street address.
Dim addr As String = txtAddress.Text
addr = HttpUtility.UrlEncode(txtAddress.Text, _
System.Text.Encoding.UTF8)
' Insert the encoded address into the base URL.
Dim url As String = URL_BASE.Replace("@ADDR@", addr)
' Insert the proper type.
Select Case cboGoogle.Text
Case "Map"
url = url.Replace("@TYPE@", "m")
Case "Satellite"
url = url.Replace("@TYPE@", "h")
Case "Terrain"
url = url.Replace("@TYPE@", "p")
End Select
' "Execute" the URL to make the default browser display
' it.
Process.Start(url)
End Sub
|
|
The following code performs similar steps for a Yahoo map. It can display a low-bandwidth version, in addition to normal, satellite, and hybrid versions.
|
|
Private Sub btnYahoo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnYahoo.Click
' The basic map URL without the address information.
Const URL_FAST As String = _
"http://maps.yahoo.com/broadband#mvt=@TYPE@&q1=@ADDR@"
Const URL_SLOW As String = _
"http://maps.yahoo.com/maps_result.php?q1=@ADDR@"
' Get broadband or dial-up base URL.
Dim url As String
If cboYahoo.Text = "Dial-Up" Then
url = URL_SLOW
Else
url = URL_FAST
End If
' URL encode the street address.
Dim addr As String = txtAddress.Text
addr = HttpUtility.UrlEncode(txtAddress.Text, _
System.Text.Encoding.UTF8)
' Insert the encoded address into the base URL.
url = url.Replace("@ADDR@", addr)
' Insert the proper type.
Select Case cboGoogle.Text
Case "Map"
url = url.Replace("@TYPE@", "m")
Case "Satellite"
url = url.Replace("@TYPE@", "s")
Case "Hybrid"
url = url.Replace("@TYPE@", "h")
End Select
' "Execute" the URL to make the default browser display
' it.
Process.Start(url)
End Sub
|
|
|
|
|
|