private static async Task GeocodeAddress(TDayAddress addr, ConcurrentBag <TDayAddress> goodAddr, ConcurrentBag <TDayAddress> badAddr, MsgOut msgOut) { var bingKey = "Aq03fYCDuaMZk0OxpH97nxHInqIsJDzab90p3twCHCk8CvlRoKjB4Xs5Msbgsvq6"; // ServiceManager.Proxy = new WebProxy("http://proxy.research.ge.com:80/"); var request = new GeocodeRequest() { Query = addr.ToAddress(), IncludeIso2 = true, IncludeNeighborhood = true, MaxResults = 25, BingMapsKey = bingKey }; //Process the request by using the ServiceManager. var response = await request.Execute(); if (response != null && response.ResourceSets != null && response.ResourceSets.Length > 0 && response.ResourceSets[0].Resources != null && response.ResourceSets[0].Resources.Length > 0) { Location foundAddr = null; foreach (var res in response.ResourceSets[0].Resources) { if (((Location)res).Address.AdminDistrict == "NY" && ((Location)res).Address.PostalCode != null && ((Location)res).Address.PostalCode.StartsWith("12")) { foundAddr = (Location)res; break; } } //var result = response.ResourceSets[0].Resources[0] as Location; if (foundAddr != null && foundAddr.MatchCodes[0].ToString() == "Good") { addr.Address1 = foundAddr.Address.AddressLine; addr.City = foundAddr.Address.Locality; addr.State = foundAddr.Address.AdminDistrict; addr.Zip = foundAddr.Address.PostalCode; var coords = foundAddr.Point.Coordinates; if (coords != null && coords.Length == 2) { addr.Lat = (float)coords[0]; addr.Lon = (float)coords[1]; } goodAddr.Add(addr); } else { badAddr.Add(addr); } //msgOut($"Geocode: {addr} => Results - Code: {result.MatchCodes[0]}\r"); } }
private static void GeocodeAddressGoogle(TDayAddress addr, ConcurrentBag <TDayAddress> goodAddr, ConcurrentBag <TDayAddress> badAddr, MsgOut msgOut) { string YOUR_API_KEY = "AIzaSyDSyz9RnSKz7AMs_EQhDVhpXvuhHmyDTGc"; string address = addr.ToAddress(); string requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?key={1}&address={0}&sensor=false", Uri.EscapeDataString(address), YOUR_API_KEY); WebRequest request = WebRequest.Create(requestUri); WebResponse response = request.GetResponse(); XDocument xdoc = XDocument.Load(response.GetResponseStream()); addr.Restaurant = GetRestaurant(addr.Zip); if (xdoc.Element("GeocodeResponse").Element("status").Value == "OK") { XElement result = xdoc.Element("GeocodeResponse").Element("result"); string [] vals = result.Element("formatted_address").ToString().Split(','); string[] tmp = vals[2].TrimStart(' ').Split(' '); vals[2] = tmp[0]; vals[3] = tmp[1]; //msgOut($"Addr: {addr.ToAddress()} Return: {addrFormat.ToString()}"); if (vals[3] != addr.Zip) { msgOut($"Changed zip: {addr.ToAddress()} => {result.Element("formatted_address").ToString()}"); } XElement locationElement = result.Element("geometry").Element("location"); addr.Address1 = vals[0]; addr.City = vals[1]; addr.State = vals[2]; addr.Zip = vals[3]; addr.Lat = float.Parse(locationElement.Element("lat").Value); addr.Lon = float.Parse(locationElement.Element("lng").Value); goodAddr.Add(addr); } else { badAddr.Add(addr); } }
private static async Task GeocodeAddress(TDayAddress addr, ConcurrentBag <TDayAddress> goodAddr, ConcurrentBag <TDayAddress> badAddr, MsgOut msgOut) { var bingKey = "Aq4xnynDuADw0OMoyrHEvCrM8VqhI8HlGIusrLdL30LGJBz71X9JtUCMcVU6mj7Y"; // ServiceManager.Proxy = new WebProxy("http://proxy.research.ge.com:80/"); var request = new GeocodeRequest() { Query = addr.ToAddress(), IncludeIso2 = true, IncludeNeighborhood = true, MaxResults = 25, BingMapsKey = bingKey }; //Process the request by using the ServiceManager. var response = await request.Execute(); if (response != null && response.ResourceSets != null && response.ResourceSets.Length > 0 && response.ResourceSets[0].Resources != null && response.ResourceSets[0].Resources.Length > 0) { Location foundAddr = null; foreach (var res in response.ResourceSets[0].Resources) { if (((Location)res).Address.AdminDistrict == "NY" && ((Location)res).Address.PostalCode != null && ((Location)res).Address.PostalCode.StartsWith("12")) { foundAddr = (Location)res; //if (((Location)res).Address.PostalCode != addr.Zip) //{ // msgOut($"Changed zip: {addr.ToAddress()} {foundAddr.Address.FormattedAddress}"); //} break; } } //var result = response.ResourceSets[0].Resources[0] as Location; addr.Restaurant = GetRestaurant(addr.Zip); if (foundAddr != null && foundAddr.MatchCodes[0].ToString() == "Good") { //addr.Address1 = foundAddr.Address.AddressLine; //addr.City = foundAddr.Address.Locality; //addr.State = foundAddr.Address.AdminDistrict; //addr.Zip = foundAddr.Address.PostalCode; var coords = foundAddr.Point.Coordinates; if (coords != null && coords.Length == 2) { addr.Lat = (float)coords[0]; addr.Lon = (float)coords[1]; } goodAddr.Add(addr); } else { badAddr.Add(addr); } //msgOut($"Geocode: {addr} => Results - Code: {result.MatchCodes[0]}\r"); } }