Share Blog

Tuesday, November 21, 2017

Find Co-ordinates (Latitude and Longitude) of an Address Location using Google Geocoding API in ASP.Net using C#

The Google Geocoding API accepts address as parameter and returns the Geographical Co-ordinates and other information in XML or JSON format.

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

<form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt_Location" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="FindCoordinates" />
<br />
<br />
<asp:GridView ID="GrdViewLocation" 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>
    </div>
    </form>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void FindCoordinates(object sender, EventArgs e)
    {
        string url = "http://maps.google.com/maps/api/geocode/xml?address=" + txt_Location.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"]);
                }
                GrdViewLocation.DataSource = dtCoordinates;
                GrdViewLocation.DataBind();
            }
        }
    }
}

No comments:

Post a Comment