Published on: 29 Jul, 2013

How to get Latitude and Longitude of an Address using Google API in ASP.Net C#?

Tumblr Digg Google Plus

In this tutorial you will learn, how you can get the latitude and longitude of an address using Google API in C#.

Google provide the following API url by returning an XML result when you passing a location address on the url.[query address]&sensor=[true or false]

ex: NSW Australia&sensor=false
sensor attribute define whether your device has a GPS sensor or not. If you develop an application through mobile that has GPS capability then you set the sensor value to true.

Let's starts with the coding in C#

// Import the following namespaces
using System.Xml;
using System.Xml.XPath;

Create the following function that will return a string containing latitude and longitude in separated comma.

public string GetLongitudeAndLatitude(string address, string sensor) {
    string urlAddress = "" + HttpUtility.UrlEncode(address) + "&sensor=" + sensor;
    string returnValue = "";
    try {
        XmlDocument objXmlDocument = new XmlDocument();
        XmlNodeList objXmlNodeList = objXmlDocument.SelectNodes("/GeocodeResponse/result/geometry/location");
        foreach (XmlNode objXmlNode in objXmlNodeList) {
            // GET LONGITUDE 
            returnValue = objXmlNode.ChildNodes.Item(0).InnerText;

            // GET LATITUDE 
            returnValue += "," + objXmlNode.ChildNodes.Item(1).InnerText;
    } catch {
        // Process an error action here if needed  
    return returnValue;

Here is how you use it, you just need to pass an address value and a sensor value.

string geoValue = GetLongitudeAndLatitude("32 george Sydney NSW Australia", "false");


06 Jun, 2019
can this adress be a value coming from a database or do i have to get the values based on object properties? am making a carpool app with and i wonder how this thing works?
07 Jun, 2019
Hi Nick, If you store the latitude and longitude in the database then it should be fine. But if you have a dynamic address, it would be better to retrieve the information directly from Google Map. Especially if you are building the a mobile app. You should use the built in location from the mobile.
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.