Click here to Skip to main content
12,897,431 members (49,952 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 9 Jun 2014

Simple Method to Convert Latitude and Longitude to Readable Address

, 9 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Function to find the exact address of particular latitude and longitude using Google maps service in C#


Usually, reverse geocoding can be done in front end using JavaScript, whereas the below method can be used in web or Windows application to convert latitude and longitude into readable address.


Google maps API provides variety of services in which I have used its reverse geocoding technique to get the XML data out of it and find the address of a particular place.

Using the Code

This method is simple and can be called from any function. You can integrate this method to your major code. It gets input as latitude and longitude and returns to you as an address.

public  string ReverseGeoLoc(string longitude, string latitude,
            out string Address_ShortName,
            out string Address_country,
            out string Address_administrative_area_level_1,
            out string Address_administrative_area_level_2,
            out string Address_administrative_area_level_3,
            out string Address_colloquial_area,
            out string Address_locality,
            out string Address_sublocality,
            out string Address_neighborhood)
        {                                                                                                                                                                                    Address_ShortName = "";
        Address_country = "";
        Address_administrative_area_level_1 = "";
        Address_administrative_area_level_2 = "";
        Address_administrative_area_level_3 = "";
        Address_colloquial_area = "";
        Address_locality = "";
        Address_sublocality = "";
        Address_neighborhood = "";     XmlDocument doc = new XmlDocument();
        {         string place = String.Format
        latlng=13.020164575569374,77.92610049247741&sensor=false");                                                                                                                                                                                                           doc.Load(place);
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
                return ("No data available for the specified location");
            {                                                                                                                                                                                       element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");

                string longname="";
                string shortname="";
                string typename ="";
                bool fHit=false;
                XmlNodeList xnList = doc.SelectNodes
                foreach (XmlNode xn in xnList)
                        longname = xn["long_name"].InnerText;
                        shortname = xn["short_name"].InnerText;
                        typename = xn["type"].InnerText;
                        fHit = true;
                        switch (typename)
                            //Add whatever you are looking for below
                              case "country":
                                    Address_country = longname;
                                    Address_ShortName = shortname;
                                }                                                                                                                                                                                case "locality":
                                    Address_locality = longname;
                                }                                                                                                                                                                              case "sublocality":
                                    Address_sublocality = longname;

case "neighborhood":
                                    Address_neighborhood = longname;
                                }                                                                                                                                                                            case "colloquial_area":
                                    Address_colloquial_area = longname;
                                }                                                                                                                                                                                case "administrative_area_level_1":
                                    Address_administrative_area_level_1 = longname;
                                }                                                                                                                                                                               case "administrative_area_level_2":
                                    Address_administrative_area_level_2 = longname;
                                }                                                                                                                                                                               case "administrative_area_level_3":
                                    Address_administrative_area_level_3 = longname;

                                fHit = false;
                        if (fHit)
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("\tL: " + longname + 
                            "\tS:" + shortname + "\r\n");
                            Console.ForegroundColor = ConsoleColor.Gray;
                    }                                                                                                                                                                         catch (Exception e)
                        //Node missing either, longname, shortname or typename
                        fHit = false;
                        Console.Write(" Invalid data: ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("\tX: " + xn.InnerXml + "\r\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                return (element.InnerText);
            }                                                                                                                                                           }
        catch (Exception ex)
            return ("(Address lookup failed: ) " + ex.Message);

Points of Interest

The code is reusable and you integrate it with any code for doing reverse geocoding from server side.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Bernhard Hiller20-Jun-14 3:36
professionalBernhard Hiller20-Jun-14 3:36 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen9-Jun-14 22:46
professionalSunasara Imdadhusen9-Jun-14 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 9 Jun 2014
Article Copyright 2014 by Godwinsmith
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid