ASP Snippets



Free Alerts

Your email will always be
private and will not be shared.

Follow us on twitter.

Zoom In | Zoom Out

Author is awarded Most Valuable Professional award by Microsoft ASP/ASP.Net

Using Google Maps API in ASP.Net

Author:Mudassar Khan

In this article, I am explaining how to use Google Map API with ASP.Net. First you need to register with the Google Maps API here and get your key from Google.

Once you get the key. You can display Google Maps on you Website using the following script that you get from Google.

<head id="Head1" runat="server">

    <title>Google Maps Example</title>

     <script type="text/javascript"


     <script type="text/javascript">

      google.load("maps", "2");

      // Call this function when the page has been loaded

      function initialize() {

        var map = new google.maps.Map2(document.getElementById("map"));

        map.setCenter(new google.maps.LatLng("<%=lat%>", "<%=lon%>"), 5);

        var point = new GPoint("<%=lon%>", "<%=lat%>");

        var marker = new GMarker(point);


        map.addControl(new GLargeMapControl());






    <form id="form1" runat="server">


        <div id="map" style="width: 400px; height: 400px"></div>





As you can see I have placed the script in the Head section of the HTML page. The above script gives your location on the map based on latitude and longitude. The map is loaded in the div with id map

Now to get Latitude and Longitude we will take help of my previous article Find Visitor's Geographic Location using IP Address in ASP.Net. You’ll notice I have used to server variables lat (latitude) and lon (longitude) whose values come from server.

Based on the IP address the web service returns the latitude and longitude refer the XML below

  <?xml version="1.0" encoding="UTF-8" ?>









    <ZipCode />






protected string lat, lon;

protected void Page_Load(object sender, EventArgs e)


    string ipaddress;

    ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (ipaddress == "" || ipaddress == null)

        ipaddress = Request.ServerVariables["REMOTE_ADDR"];

    DataTable dt = GetLocation(ipaddress);

    if (dt != null)


        if (dt.Rows.Count > 0)


            lat = dt.Rows[0]["Latitude"].ToString();

            lon = dt.Rows[0]["Longitude"].ToString(); 





private DataTable GetLocation(string ipaddress)


    //Create a WebRequest

    WebRequest rssReq = WebRequest.Create(""

        + ipaddress);


    //Create a Proxy

    WebProxy px = new WebProxy(""

        + ipaddress, true);


    //Assign the proxy to the WebRequest

    rssReq.Proxy = px;


    //Set the timeout in Seconds for the WebRequest

    rssReq.Timeout = 2000;



        //Get the WebResponse

        WebResponse rep = rssReq.GetResponse();


        //Read the Response in a XMLTextReader

        XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());


        //Create a new DataSet

        DataSet ds = new DataSet();


        //Read the Response into the DataSet


        return ds.Tables[0];




        return null;




Protected lat As String, lon As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)  Handles Me.Load

        Dim ipaddress As String

        ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

        If ipaddress = "" OrElse ipaddress Is Nothing Then

            ipaddress = Request.ServerVariables("REMOTE_ADDR")

        End If

        Dim dt As DataTable = GetLocation(ipaddress)

        If dt IsNot Nothing Then

            If dt.Rows.Count > 0 Then

                lat = dt.Rows(0)("Latitude").ToString()

                lon = dt.Rows(0)("Longitude").ToString()

            End If

        End If

End Sub


Private Function GetLocation(ByVal ipaddress As String) As DataTable

        'Create a WebRequest

        Dim rssReq As WebRequest = WebRequest. _

           Create("" & ipaddress)


        'Create a Proxy

        Dim px As New WebProxy("" _

        & ipaddress, True)


        'Assign the proxy to the WebRequest

        rssReq.Proxy = px


        'Set the timeout in Seconds for the WebRequest

        rssReq.Timeout = 2000


            'Get the WebResponse

            Dim rep As WebResponse = rssReq.GetResponse()


            'Read the Response in a XMLTextReader

            Dim xtr As New XmlTextReader(rep.GetResponseStream())


            'Create a new DataSet

            Dim ds As New DataSet()


            'Read the Response into the DataSet


            Return ds.Tables(0)


            Return Nothing

        End Try

End Function


As you can see in the above code snippet I get the latitude and longitude from the XML Response in the variables lat and lon which I’ll used to pass values to the JavaScript function of the Google API.

This completes the article. Download the sample source from the link below. (4.26 kb)
Posted: Apr 15 2009, 05:24 by Mudassar Khan | Comments (18) RSS comment feed |
Filed under: ASP.Net | C# | JavaScript | VB.Net

Views: 7794
Page copy protected against web site content infringement by Copyscape

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share


Community News

Web Hosting SpotLight


For consulting and work related queries click here.


Advertise with us. For more details click here.


Please provide your valuable suggesstions here.

This Site is hosted on Web Hosting