ARTICLE

How to find your search location using VB.NET

Posted by Purushottam Rathore Articles | Visual Basic 2010 August 07, 2009
Here I am going to discuss a simple application to find your search location in the google map using VB.NET.
Download Files:
 
Reader Level:

Here I am going to discuss a simple application to find your search location in the google map, by street, city, state, zip code wise. You can also search your location by Latitude and Longitude.

<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <
title>Untitled Page</title>
</
head>
<
body>
    <
form id="form1" runat="server">
        <
div>
            <
table width="300px" cellpadding="2" cellspacing="2" style="border: 1px solid maroon;">
                <
tr>
                    <
td colspan="2">
                       
&nbsp;<b>Search by Street, City, State and ZipCode</b
>
                    </td>
                </tr>
                <tr>
                    <td>
                        Street
                    </td>
                    <td>
                        <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                    City
                    </td>
   
                <td>
   
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                    State
                    </td>
   
                <td>
   
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                    ZipCode
                    </td>
   
                <td>
   
                    <asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                </td>
   
                <td>
   
                    <asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" /><br />
   
                </td>
   
            </tr>
   
        </table>
   
    <div>&nbsp;
        </div>
   
        <table width="300px" cellpadding="2" cellspacing="2" style="border: 1px solid maroon;">
   
            <tr>
   
                <td colspan="2">
   
                    &nbsp;<b>Search by Latitude and Longitude</b>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                    Latitude</td>
   
                <td>
   
                    <asp:TextBox ID="txtLat" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                    Longitude</td>
   
                <td>
   
                    <asp:TextBox ID="txtLong" runat="server"></asp:TextBox>
   
                </td>
   
            </tr>
   
            <tr>
   
                <td>
   
                </td>
   
                <td>
   
                    <asp:Button ID="ButtonLatLong" runat="server" Text="Search by Lat Long" OnClick="ButtonLatLong_Click" CausesValidation="false" />
   
                </td>
   
            </tr>
   
        </table>
   
    </div>
   
</form>
</
body>
</
html>

Add reference as follows:

Add.jpg

Figure 1:

addreference.jpg

Figure 2:

Imports
System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms

Partial
Public Class _Default
    Inherits System.Web.UI.Page
    Private url As String

   
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

    Protected Sub ButtonSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
        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=")

            If txtStreet.Text <> String.Empty Then
                street = txtStreet.Text.Replace(" "c, "+"c)
                queryAddress.Append(street + ","c + "+"c)
            End If

            If txtCity.Text <> String.Empty Then
                city = txtCity.Text.Replace(" "c, "+"c)
                queryAddress.Append(city + ","c + "+"c)
            End If

            If txtState.Text <> String.Empty Then
                state = txtState.Text.Replace(" "c, "+"c)
                queryAddress.Append(state + ","c + "+"c)
            End If

            If txtZipCode.Text <> String.Empty Then
                zip = txtZipCode.Text.ToString()
                queryAddress.Append(zip)
            End If

            url = queryAddress.ToString()
            Response.Redirect(url, False)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve Map")
        End Try

    End Sub

    Protected Sub ButtonLatLong_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txtLat.Text = String.Empty OrElse txtLong.Text = String.Empty Then
            MessageBox.Show("Supply a latitude and longitude value", "Missing Data")
            Exit Sub
        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=")

            If txtLat.Text <> String.Empty Then
                lat = txtLat.Text
                queryAddress.Append(lat & "%2C")
            End If

            If txtLong.Text <> String.Empty Then
                lon = txtLong.Text
                queryAddress.Append(lon)
            End If

            url = queryAddress.ToString()
            Response.Redirect(url, False)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Error")
        End Try
    End Sub
End
Class

Output:

Search.jpg

Figure 3:


Suppose you want to search Radisson hotel in Noida, India as follows:

Search2.jpg

Figure 4:

You will get the following search result with map.

result.jpg

Figure 5:

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor