Thursday, 31 March 2011

Search City in asp.net Using google map


Introduction
 In this article, i will show to you how to search city map using Google map in ASP.NET.When you are see now days all the web contacts address are attached with map of the city. the Google has been provided a incredible map service to find the address, zipcode, state through the external web application. So we will implement a web application like find the address with Google map in asp.net.
Implementation
Create a web application project using visual studio and then add following html code in your default page.
<table width="300px" cellpadding="2" cellspacing="2" style="border: 1px solid maroon;">
                <tr>
                    <td colspan="2">
                        &nbsp;<b>Search Your City Map By Adding  City, State and ZipCode FRom Google Map</b>
                    </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>      
The design is finished, now we have to write server side to do the search for address or zipcode to find in google map.
C# Code
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 Default2 : System.Web.UI.Page
{

    string url;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        try
        {
        
            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 (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");
        }
    }
}
that's all. i hope its help to all you attach map with your project.

2 comments: