Free Information Technology Magazines and eBooks

Tuesday, April 07, 2009

VB.NET: Using Google Maps on your application

During my college days we created a project called "Electronic Map Navigator". Its function is to locate the easiest route from Point A to Point B. Those days, creating map enabled application was still a complicated task but not anymore. Today I guess it's as easy as creating your first hello program. The task made easier by the availability of internet, Google Maps, Google Earth and Virtual Earth. On this article, I will show you how to implement google maps on your VB.NET applications (without using Maps API).

  • Add "Imports System.Text" statement on your VB.NET project
  • In your main form, add text boxes to hold street, city, state and zip
  • Add search button for user to click when searching the map. On it's click event put the following codes:


  • Try
    Dim streetaddr As String = ""
    Dim cityaddr As String = ""
    Dim stateaddr As String = ""
    Dim zipaddr As String = ""
    Dim AddrToSearch As New StringBuilder()
    AddrToSearch.Append("http://maps.google.com/maps?q=")

    ' if there is street entry
    If txtStreet.Text <> "" Then
    streetaddr = txtStreet.Text.Replace(" ", "+")
    AddrToSearch.Append(street + "," & "+")
    End If

    ' if there is city entry
    If txtCity.Text <> "" Then
    cityaddr  = txtCity.Text.Replace(" ", "+")
    AddrToSearch.Append(city + "," & "+")
    End If

    ' if there is state
    If txtState.Text <> "" Then
    stateaddr = txtState.Text.Replace(" ", "+")
    AddrToSearch.Append(state + "," & "+")
    End If

    ' if there is zip code
    If txtZipCode.Text <> "" Then
    zipaddr = txtZipCode.Text.ToString()
    AddrToSearch.Append(zip)
    End If

    ' pass the AddrToSearch value to web browser control
    wbrowser.Navigate(AddrToSearch.ToString())

    Catch ex As Exception

    MessageBox.Show(ex.Message.ToString(), "Problem encountered while retrieving google map")

    End Try

  • Aside from actual street address of the map, you can also search the map by latitude and longitude


  • Try
    Dim latitude As String = ""
    Dim longitude As String = ""
    Dim AddrToSearch As New StringBuilder()
    AddrToSearch.Append("http://maps.google.com/maps?q=")

    ' if there is latitude
    If txtLatitude.Text <> "" Then
    latitude = txtLatitude.Text
    AddrToSearch.Append(latitude & "%2C")
    End If

    ' if there is longitude
    If txtLongitude .Text <> "" Then
    longitude = txtLongitude .Text
    AddrToSearch.Append(longitude)
    End If

    ' pass the AddrToSearch value to web browser control
    wbrowser.Navigate(AddrToSearch.ToString())

    Catch ex As Exception

    MessageBox.Show(ex.Message.ToString(), "Problem encountered while retrieving google map")

    End Try



Since using this method does not require or needs any Google Maps API, you have little or no control on view map controls. These controls are already provided by Google Maps inside your web browser control.


3 comments:

Mario Hidalgo said...

sabes alguna manera de usar las funciones de google maps con un control web browser en vb.net desktop?

jeo said...

Hi Fryan! Im Jeo a college student in the Philippines and iam working now on my thesis. I am now making a digital map we have the same concept (Its function is to locate the easiest route from Point A to Point B) Can you please mail me your work during in your college days (electronic map navigator) ? i really need help on this sir pls? jeoremil@yahoo.com pls sir! I really need help :| thank u in advance hope you'll reply +++++karma to u thanks again sir! :)

jeo said...

Hi Fryan! Im Jeo a college student in the Philippines and iam working now on my thesis. I am now making a digital map we have the same concept (Its function is to locate the easiest route from Point A to Point B) Can you please mail me your work during in your college days (electronic map navigator) ? i really need help on this sir pls? jeoremil@yahoo.com pls sir! I really need help :| thank u in advance hope you'll reply +++++karma to u thanks again sir! :)