IT Advice and Code Snippets

    Paul Gough

 
home
 


Google maps Geocoder sample in vb aspx .net

This is the code to create a Google Maps Geocoder (address lookups) only using code behind aspx.vb in .net. I wanted to see if it was possible to do a functional maps application without using any javascript.

I am using 2 activex controls

Subgurim.Controles map will display the map and can be downloaded at
http://en.googlemaps.subgurim.net/

GMapGeocoder will handle the addess lookup to get the location coordinates
I could not use the Subgurim.Controles geocoder option because it will only return one address 
Download this dll http://www.goughit.com/GMapGeocoder.dll and add reference to your project.
Thanks to Sharmil Y Desai for GMapGeocoder. http://www.codeproject.com/KB/custom-controls/GMapGeocoder.aspx

You will also have to get a google maps key here http://code.google.com/apis/maps/signup.html 
In the aspx page and the aspx.vb code behind page replace "Your Key code" with it.

You can try out the example here http://www.goughit.com/maps/GoogleMap.aspx

Ok here is the code I used in the example

Put this at the top of the aspx page
<%@ Register Assembly="GMaps" Namespace="Subgurim.Controles" TagPrefix="cc1" %>

And then add this table to your aspx page

        <table width="100%" cellspacing="7" cellpadding="0">
            <tr>
                <td align="left" valign="top" class="style1">
                    <asp:Label ID="LblRes" runat="server" Font-Names="Arial" Font-Size="10pt" TabIndex="4"></asp:Label>
                    <br />
                    <cc1:GMap ID="GMap1" runat="server" GZoom="2" Height="500px" Key="Your Key code"
                        Width="600px" BorderColor="#000066" BorderStyle="Groove" BorderWidth="2px" enableHookMouseWheelToZoom="True"
                        TabIndex="5" ToolTip="double click or wheel mouse to zoom" mapType="Hybrid" />
                </td>
                <td align="left" valign="top" class="style2">
                    <p style="font-family: Arial; font-size: 10pt">
                        Enter street or town</p>
                    <p>
                        <asp:TextBox ID="TextBox1" runat="server" Font-Names="Arial" Font-Size="10pt" TabIndex="3"
                            Width="251px"></asp:TextBox>
                    </p>
                    <p>
                        <asp:Button ID="Button1" runat="server" Text="Search" Width="100px" />
                    </p>
                    <p>
                        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="260px" Font-Names="Arial"
                            Font-Size="8pt" TabIndex="2" Width="250px"></asp:ListBox>
                    </p>
                    <p>
                        <asp:Button ID="BtnClear" runat="server" Text="Clear" TabIndex="1" Width="100px"
                            Style="height: 26px" />
                    </p>
                </td>
            </tr>
        </table>

And now the aspx.vb code behind

Namespace imports as follows

Imports Subgurim.Controles
Imports GMapGeocoder

And now the code behind

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' loads the map type menu
        GMap1.mapType = GMapType.GTypes.Physical
        GMap1.addMapType(GMapType.GTypes.Physical)
        GMap1.addControl(New GControl(GControl.preBuilt.MapTypeControl))
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' run geocoder and loop through the results loading them into a listbox
        Dim results As GMapGeocoder.Containers.Results = GMapGeocoder.Util.Geocode(TextBox1.Text, "Your Key code")
        For i = 0 To results.Addresses.Count - 1 'loop through all the addresses returned
            Dim latLng As New GLatLng(results.Addresses(i).Coordinates.Latitude, results.Addresses(i).Coordinates.Longitude)
            ListBox1.Items.Add(i & " " & results.Addresses(i).FullText & " --- " & results.Addresses(i).Coordinates.Latitude & "," & results.Addresses(i).Coordinates.Longitude)
            NewMarker(results.Addresses(i).Coordinates.Latitude, results.Addresses(i).Coordinates.Longitude, results.Addresses(i).FullText, i)
        Next
    End Sub

    Sub NewMarker(ByVal lat As String, ByVal lng As String, ByVal cInfo As String, ByVal i As String)
        ' adds markers to the map
        Dim latLng As New GLatLng(lat, lng)
        Dim icon As New GMarker(latLng)
        ' the info window will html tags ahref mailto etc..
        Dim window As New GInfoWindow(icon, "<b>" & cInfo & "</b", False, GListener.[Event].mouseover)
        GMap1.addInfoWindow(window)
    End Sub


    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ' clicking the item in the listbox zooms in the map to the marker
        Dim slatLng As String
        slatLng = ListBox1.Text.Substring(ListBox1.Text.IndexOf("---") + 4)
        LblRes.Text = ListBox1.SelectedItem.Text
        Dim latLng As New GLatLng(slatLng.Substring(0, slatLng.IndexOf(",")), slatLng.Substring(slatLng.IndexOf(",") + 1))
        GMap1.setCenter(latLng, 7)
    End Sub

    Protected Sub BtnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        ' clears the listbox and markers
        GMap1.reset()
        ListBox1.Items.Clear()
        Dim latLng As New GLatLng(0, 0)
        GMap1.setCenter(latLng, 2)
    End Sub

Ok thats all. Comment me if you have any questions.



A Simple Blog engine developed with .NET

Get IP and counrtry of your webpage visitors

Google maps Geocoder sample in vb aspx .net

Send an email to a mobile phone

Comments

Name