Utility per geolocalizzazione

Tags: C#, GeoLocalization

Una piccola classe che consente di ricavare latitudine e longitude a partire da un indirizzo e viceversa.

public class GeoLocation
    {
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
    }

    public class GeoManager
    {
        public static GeoLocation GetCoordinates(string address)
        {
            GeoLocation geoLocation = new GeoLocation();
            try
            {
                string url = string.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false&language=it", address);
                XmlDocument doc = new XmlDocument();
                doc.Load(url);
                XmlNodeList nodes = doc.SelectNodes("//location");

                if (nodes == null)
                {
                    Log.Info(string.Format("Nodes is null in GeoManager.GetCoordinates('{0}')", address));
                    return geoLocation;
                }
                foreach (XmlNode node in nodes)
                {
                    XmlNode lat = node.SelectSingleNode("lat");
                    XmlNode lng = node.SelectSingleNode("lng");
                    if (lat == null || lng == null)
                    {
                        continue;
                    }
                    geoLocation.Latitude = decimal.Parse(lat.InnerText, CultureInfo.InvariantCulture);
                    geoLocation.Longitude = decimal.Parse(lng.InnerText, CultureInfo.InvariantCulture);
                    break;
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("GetCoordinates('{0}')", address), ex);
            }
            return geoLocation;
        }

        public static GeoLocation GetCoordinates(string street, string city, string state)
        {
            string url = string.Format("http://maps.google.com/maps/api/geocode/xml?address={0},{1},{2}&sensor=false&language=it", street, city, state);
            XmlDocument doc = new XmlDocument();
            doc.Load(url);
            XmlNodeList nodes = doc.SelectNodes("//location");

            GeoLocation geoLocation = new GeoLocation();
            if (nodes == null)
            {
                Log.Info(string.Format("Nodes is null in GeoManager.GetCoordinates('{0}','{1}','{2}')", street, city, state));
                return geoLocation;
            }

            foreach (XmlNode node in nodes)
            {
                XmlNode lat = node.SelectSingleNode("lat");
                XmlNode lng = node.SelectSingleNode("lng");
                if (lat == null || lng == null)
                {
                    continue;
                }
                geoLocation.Latitude = decimal.Parse(lat.InnerText,  CultureInfo.InvariantCulture);
                geoLocation.Longitude = decimal.Parse(lng.InnerText, CultureInfo.InvariantCulture);
            }

            return geoLocation;
        }

        public static string GetAddress(string latitude, string longitude)
        {
            try
            {
                latitude = latitude.Replace(",", ".");
                longitude = longitude.Replace(",", ".");
                string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false&language=it", latitude, longitude);
                XmlDocument doc = new XmlDocument();
                doc.Load(url);
                var nodes = doc.SelectSingleNode("//formatted_address");

                if (nodes == null)
                    return string.Empty;

                string result = nodes.InnerText;
                return result;
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("GetAddress({0},{1})", latitude, longitude), ex);
            }
            return string.Empty;
        }
    }

Add a Comment