public DataContracts.DC_GeoLocation GetGeoLocationByLatLng(DataContracts.DC_Address.DC_Address_GeoCode AG) { using (BusinessLayer.BL_GeoLocation obj = new BL_GeoLocation()) { return(obj.GetGeoLocationByLatLng(AG)); } }
public DataContracts.DC_GeoLocation GetGeoLocationByLatLng(DataContracts.DC_Address.DC_Address_GeoCode AG) { try { string LatLng = String.Empty; LatLng = AG.Latitude.ToString() + "," + AG.Longitude.ToString(); DataContracts.DC_GeoLocation mapdata = null; var request = (HttpWebRequest)WebRequest.Create("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + LatLng + "&key=" + System.Configuration.ConfigurationManager.AppSettings["GoogleKey"].ToString()); var proxyAddress = System.Configuration.ConfigurationManager.AppSettings["ProxyUri"]; if (System.Configuration.ConfigurationManager.AppSettings["ProxyUri"] != null) { WebProxy myProxy = new WebProxy(); Uri newUri = new Uri(proxyAddress); // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set. myProxy.Address = newUri; // Create a NetworkCredential object and associate it with the // Proxy property of request object. //myProxy.Credentials = new NetworkCredential(username, password); request.Proxy = myProxy; } request.KeepAlive = false; HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) //response.StatusDescription { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); mapdata = JsonConvert.DeserializeObject <DataContracts.DC_GeoLocation>(responseFromServer); if (mapdata != null) { using (ConsumerEntities context = new ConsumerEntities()) { GoogleGeoCode GC = new GoogleGeoCode(); GC.GoogleGeoCode_Id = Guid.NewGuid(); GC.Product_Id = AG.Product_Id; GC.JobType = "ProductGeoLookup_ByLatLng"; GC.Input = request.Address.AbsoluteUri; GC.OutPut = responseFromServer; context.GoogleGeoCodes.Add(GC); context.SaveChanges(); } } } //DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(DataContracts.DC_GeoLocation)); //var result = obj.ReadObject(dataStream) as DataContracts.DC_GeoLocation; response.Close(); return(mapdata); } catch { throw new FaultException <DataContracts.DC_ErrorStatus>(new DataContracts.DC_ErrorStatus { ErrorMessage = "Error while searching address", ErrorStatusCode = System.Net.HttpStatusCode.InternalServerError }); } }