Exemplo n.º 1
0
        public static async Task <GeocoderLocation> GoeCodeAsync(string query)
        {
            GeocoderLocation geoLocation = null;
            await Task.Run(() => { geoLocation = GoeCodeSync(query); });

            return(geoLocation);
        }
Exemplo n.º 2
0
        public static GeocoderLocation GoeCodeSync(string query)
        {
            GeocoderLocation geoLocation = null;

            try
            {
                String mapAPIURL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false";
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings[Constants.GoogleMapAPIURL]))
                {
                    mapAPIURL = ConfigurationManager.AppSettings[Constants.GoogleMapAPIURL];
                }

                WebRequest webRequest = WebRequest.Create(mapAPIURL + "&address=" + HttpUtility.UrlEncode(query));
                #region Proxy Settings
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings[Constants.ProxyAddress]))
                {
                    WebProxy webProxy = new WebProxy(ConfigurationManager.AppSettings[Constants.ProxyAddress], false);
                    if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings[Constants.ProxyUserName]) &&
                        !String.IsNullOrEmpty(ConfigurationManager.AppSettings[Constants.ProxyPassword]))
                    {
                        webProxy.Credentials = new NetworkCredential(ConfigurationManager.AppSettings[Constants.ProxyUserName],
                                                                     ConfigurationManager.AppSettings[Constants.ProxyPassword]);
                    }
                    webRequest.Proxy = webProxy;
                }
                #endregion
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        XDocument document = XDocument.Load(new StreamReader(stream));

                        XElement longitudeElement = document.Descendants("lng").FirstOrDefault();
                        XElement latitudeElement  = document.Descendants("lat").FirstOrDefault();

                        if (longitudeElement != null && latitudeElement != null)
                        {
                            geoLocation = new GeocoderLocation
                            {
                                Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                                Latitude  = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                            };
                        }
                        XElement statusElement = document.Descendants("status").FirstOrDefault();
                        if (statusElement != null && !String.IsNullOrEmpty(statusElement.Value) && statusElement.Value.ToLower() != "ok")
                        {
                            //Utility.InsertIntoErrorLog("Utility.GoeCodeSync", Utility.PrepareStringForDB("GeoCoding Response for '" + query + "' is: ") + statusElement.Value, "Admin");
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //Utility.InsertIntoErrorLog(exp.Source, exp.Message + "\r\n" + exp.StackTrace, "Admin");
            }
            return(geoLocation);
        }