Monday, September 30, 2013

Get Geo location coordinates by submitting a String value (an address) in C#


String address is my string value where by passing an address.
Inside the foreach loop i can get the Longitude and Latitude.

To use this code you need to have below references available in your reference folder on the solution explorer.
using System.Xml;
using System.Web;

using System.Net;





public  void GetGeoLocation()
{
         string address = "Colombo 03, Colombo";


            string urlAddress = "http://maps.googleapis.com/maps/api/geocode/xml?address=" +          HttpUtility.UrlEncode(address) + "&sensor=false";


  try
    {
         XmlDocument objXmlDocument = new XmlDocument();
                    objXmlDocument.Load(urlAddress);


         XmlNodeList objXmlNodeList = objXmlDocument.SelectNodes("/GeocodeResponse/result/geometry/location");

 foreach (XmlNode objXmlNode in objXmlNodeList)
      {
        // GET LONGITUDE
      string Latitude = objXmlNode.ChildNodes.Item(0).InnerText;
        // GET LATITUDE
      string Longitude = objXmlNode.ChildNodes.Item(1).InnerText;
      }

                 
     }
    catch (Exception)
                { }

 }





No comments:

Post a Comment