Find Latitude and Longitude of an Address Location Using Google API in Asp .Net using c#

04 / Feb / 2016 by Anshul Verma 3 comments

This Blog explains how to find the Latitude / Longitude co-ordinates by using Google Geocoding API. This API will return latitude and longitude with respect to a address of a location in Asp .Net.

We have to pass locations address as a parameter in address query strings in the Google API URL and returns to the Geographical Co-ordinates and other information in XML or JSON format.

How to send request to get coordinate by using Geocoding API

Through above URL, we can find the latitude and longitude by passing the particular address parameter in query string named “address”. The output may be either of the following value:

  • JSON
  • XML

We can use Google Geocoding API to convert an address to its Latitude or longitude by creating HTTP request.

C# Code

Add following namespace

using System.IO;

using System.Net;

Stirng strAddress  = ”Noida 201301,India”;

String url=”+ strAddress +”&sensor=false;

This is given below function named as “GetdtLatLong” in which we will pass input parameter the url of API as given above and it will return  the output as data table

Public DataTable GetdtLatLong(string url)


DataTable dtGMap=null;

WebRequest request = WebRequest.Create(url);

using (WebResponse response = (HttpWebResponse)request.GetResponse())


using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))


DataSet dsResult = new DataSet();


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”]);


dtGMap= dtCoordinates;

returns       dtGMap;

} }

Result: The output of dtGMap data table which is fetched by google API will be as



comments (3)

  1. Sonu Pandey


    Object reference not set to an instance of an object.

    This code is giving error in this line
    foreach (DataRow row in dsResult.Tables[“result”].Rows)



Leave a comment - mohan singh baghel Cancel reply