Monday 17 April 2017

Get formatted address based on latitude and longitude in c#

Required namespace:

using System.Net;
using System.Xml.Linq;

Function for getting address:

public static string GetAddress(double latitude, double longitude)
        {
            try
            {
                var requestUri = string.Format("http://maps.google.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
                var request = WebRequest.Create(requestUri);
                var response = request.GetResponse();
                var xdoc = XDocument.Load(response.GetResponseStream());
                var result = xdoc.Element("GeocodeResponse").Element("result");
                return result.Element("formatted_address").Value;
            }
            catch
            {
                return "";
            }
        }

Function call:

string address=GetAddress(40.714224,-73.961452);

No comments:

Post a Comment