Click here to Skip to main content
Click here to Skip to main content

Simple Method to Convert Latitude and Longitude to Readable Address

, 9 Jun 2014
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#

Introduction

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.

Background

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();
        try
        {         string place = String.Format
        ("http://maps.googleapis.com/maps/api/geocode/xml?
        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");
            }
            else
            {                                                                                                                                                                                       element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");

                string longname="";
                string shortname="";
                string typename ="";
                bool fHit=false;
                XmlNodeList xnList = doc.SelectNodes
                ("//GeocodeResponse/result/address_component");
                foreach (XmlNode xn in xnList)
                {
                    try
                    {
                        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;
                                    break;
                                }                                                                                                                                                                                case "locality":
                                {
                                    Address_locality = longname;
                                    break;
                                }                                                                                                                                                                              case "sublocality":
                                {
                                    Address_sublocality = longname;
                                    break;
                                }

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

default:
                                fHit = false;
                                break;
                        }
                        if (fHit)
                        {
                            Console.Write(typename);
                            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.

License

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

Share

About the Author

Godwinsmith
Software Developer (Junior)
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 PinprofessionalBernhard Hiller20-Jun-14 3:36 
GeneralMy vote of 5 PinpremiumSunasara Imdadhusen9-Jun-14 22:46 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 9 Jun 2014
Article Copyright 2014 by Godwinsmith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid