How to find your search location using asp.net



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. or you can also search your location by Latitude and Longitude.

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>Google map</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"/>
               
</td>
            </tr>
            <tr>
                <td>City</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"/>
               
</td>
            </tr>
            <tr>
                <td>State</td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"/>
               
</td>
            </tr>
            <tr>
                <td>ZipCode</td>
                <td>
                    <asp:TextBox ID="txtZipCode" runat="server"/>
               
</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"/>
            </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 like as follows:

Add.JPG

Figure 1:

addreference.JPG

Figure 2:

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

public
partial class _Default : System.Web.UI.Page
{
    string url;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
   
    try
        {
            string street = string.Empty;
            string city = string.Empty;
            string state = string.Empty;
            string zip = string.Empty;

   
        StringBuilder queryAddress = new StringBuilder();
            queryAddress.Append("http://maps.google.com/maps?q=");
       
            if (txtStreet.Text != string.Empty)
            {
                street = txtStreet.Text.Replace(' ', '+');
                queryAddress.Append(street + ',' + '+');
            }

            if (txtCity.Text != string.Empty)
            {
                city = txtCity.Text.Replace(' ', '+');
                queryAddress.Append(city + ',' + '+');
            }

            if (txtState.Text != string.Empty)
            {
                state = txtState.Text.Replace(' ', '+');
                queryAddress.Append(state + ',' + '+');
            }

            if (txtZipCode.Text != string.Empty)
            {
                zip = txtZipCode.Text.ToString();
                queryAddress.Append(zip);
            }

            url = queryAddress.ToString();
            Response.Redirect(url, false);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve Map");
        }
    }

    protected void ButtonLatLong_Click(object sender, EventArgs e)
    {
        if (txtLat.Text == string.Empty || txtLong.Text == string.Empty)
        {
            MessageBox.Show("Supply a latitude and longitude value", "Missing Data");
            return;
        }
        try
        {
            string lat = string.Empty;
            string lon = string.Empty;
            StringBuilder queryAddress = new StringBuilder();
            queryAddress.Append("http://maps.google.com/maps?q=");
            if (txtLat.Text != string.Empty)
            {
                lat = txtLat.Text;
                queryAddress.Append(lat + "%2C");
            }
            if (txtLong.Text != string.Empty)
            {
                lon = txtLong.Text;
                queryAddress.Append(lon);
            }
            url = queryAddress.ToString();
            Response.Redirect(url, false);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }
    }
}


Output:

Search.JPG

Figure 3:


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

 Search2.JPG

Figure 4:

you will get the following search result with map. 

result.JPG 

Figure 5:


Similar Articles