In this short code snippet article I will explain how to find the co-ordinates i.e. Latitude and Longitude of an Address Location using Google Maps Geocoding API.
The Google Geocoding API accepts address as parameter and returns the Geographical Co-ordinates and other information in XML or JSON format.
 

 
HTML Markup
The HTML markup consists of a TextBox and a Button for searching and a GridView control to display the results.
<asp:TextBox ID="txtLocation" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="FindCoordinates" />
<br />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Address" HeaderText="Address" />
        <asp:BoundField DataField="Latitude" HeaderText="Latitude" />
        <asp:BoundField DataField="Longitude" HeaderText="Longitude" />
    </Columns>
</asp:GridView>

 

 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Net;
using System.Text;
using System.Data;
 
VB.Net
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Data

 

 
Making call to Google Geocoding API to fetch Co-ordinates (Latitude and Longitude) of an Address Location in ASP.Net
The following click event handler is executed when the Search Button is clicked, the address from the TextBox is passed to the Google Geocoding API Service using WebRequest.
The received response is an XML string which is read into a DataSet using StreamReader.
Since the XML returned from Google contains information in multiple tables, I have extracted the relevant information and inserted into a DataTable and finally the DataTable is bound to the GridView control.
C#
protected void FindCoordinates(object sender, EventArgs e)
{
    string url = "http://maps.google.com/maps/api/geocode/xml?address=" + txtLocation.Text + "&sensor=false";
    WebRequest request = WebRequest.Create(url);
    using (WebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            DataSet dsResult = new DataSet();
            dsResult.ReadXml(reader);
            DataTable dtCoordinates = new DataTable();
            dtCoordinates.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Address", typeof(string)),
                        new DataColumn("Latitude",typeof(string)),
                        new DataColumn("Longitude",typeof(string)) });
            foreach (DataRow row in dsResult.Tables["result"].Rows)
            {
                string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
                DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];
                dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]);
            }
            GridView1.DataSource = dtCoordinates;
            GridView1.DataBind();
        }
    }
}
 
VB.Net
Protected Sub FindCoordinates(sender As Object, e As EventArgs)
    Dim url As String = "http://maps.google.com/maps/api/geocode/xml?address=" + txtLocation.Text + "&sensor=false"
    Dim request As WebRequest = WebRequest.Create(url)
    Using response As WebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Using reader As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
            Dim dsResult As New DataSet()
            dsResult.ReadXml(reader)
            Dim dtCoordinates As New DataTable()
            dtCoordinates.Columns.AddRange(New DataColumn(3) {New DataColumn("Id", GetType(Integer)), New DataColumn("Address", GetType(String)), New DataColumn("Latitude", GetType(String)), New DataColumn("Longitude", GetType(String))})
            For Each row As DataRow In dsResult.Tables("result").Rows
                Dim geometry_id As String = dsResult.Tables("geometry").[Select]("result_id = " + row("result_id").ToString())(0)("geometry_id").ToString()
                Dim location As DataRow = dsResult.Tables("location").[Select](Convert.ToString("geometry_id = ") & geometry_id)(0)
                dtCoordinates.Rows.Add(row("result_id"), row("formatted_address"), location("lat"), location("lng"))
            Next
            GridView1.DataSource = dtCoordinates
            GridView1.DataBind()
        End Using
    End Using
End Sub

 

 
Screenshots
When the searched location is an exact match only one result is returned.
Find Co-ordinates (Latitude and Longitude) of an Address Location using Google Geocoding API in ASP.Net using C# and VB.Net
When the searched location has multiple matches then we get multiple results
Find Co-ordinates (Latitude and Longitude) of an Address Location using Google Geocoding API in ASP.Net using C# and VB.Net
Demos

 
Downloads