public Address GetGpsAddress(string countryName, int countryId, string Lat, string Long) { string url = "http://maps.google.com/maps/api/geocode/json?latlng=" + Lat + "," + Long + "&sensor=false"; var request = (HttpWebRequest)HttpWebRequest.Create(url); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse)); GeoResponse r = null; try { r = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream()); } catch (Exception ee) { string str = ee.Message; } Address _location = new Address(); if (r.Status == "OK") { _location.SearchResult = (int)LocationSearchResultEnum.Found; var v = r.Results.ToList(); GeoResponse.CResult rr; //rr = r.Results.Where(c => c.AddressComponents.Any(x => c.Type.Contains("street_number"))).FirstOrDefault(); rr = r.Results.Where(c => c.AddressComponents.Any(x => x.Type.Contains("street_number"))).FirstOrDefault(); if (rr == null) { rr = r.Results.Where(c => c.AddressComponents.Any(x => x.Type.Contains("route"))).FirstOrDefault(); } if (rr == null) { _location.SearchResult = (int)LocationSearchResultEnum.NotFound; } else { _location.LocationType = rr.Geometry.LocationType; string sNum = "", sStreet = "", sCity = "", sState = ""; GeoResponse.CResult.GeocodeAddressComponent[] addrc = rr.AddressComponents; List <GoogleAddressPartsToDB> addrDbProps = GetCountryGoogleProps(Convert.ToInt32(countryId)); if ((addrDbProps != null) && (addrDbProps.Count > 0)) { List <GeoResponse.CResult.GeocodeAddressComponent> responseComponents = addrc.ToList(); sState = GetAddressPartString(2, responseComponents, addrDbProps); sCity = GetAddressPartString(3, responseComponents, addrDbProps); sStreet = GetAddressPartString(4, responseComponents, addrDbProps); sNum = GetAddressPartString(5, responseComponents, addrDbProps); } else { for (int i = 0; i < addrc.Length; i++) { string sprop = addrc[i].Type[0]; if (sprop == "street_number") { sNum = addrc[i].ShortName; } if (sprop == "route") { sStreet = addrc[i].ShortName; } if (sprop == "administrative_area_level_1") { sState = addrc[i].ShortName; } if (sprop == "locality") { sCity = addrc[i].ShortName; } } } //if ((addrDbProps != null) && (addrDbProps.Count > 0)) _location.Building = sNum; _location.Street = sStreet; _location.City = sCity; _location.State = sState; _location.Country = countryName; _location.FullAddressString = countryName; if (sState.Length > 0) { _location.FullAddressString += " "; _location.FullAddressString += sState; } _location.FullAddressString = _location.FullAddressString + " " + sCity + " " + sStreet; if (sNum.Length > 0) { _location.FullAddressString += " "; _location.FullAddressString += sNum; } } } else if (r.Status == "ZERO_RESULTS") { _location.SearchResult = (int)LocationSearchResultEnum.NotFound; } else if (r.Status == "REQUEST_DENIED") { _location.SearchResult = (int)LocationSearchResultEnum.RequestDenied; } else if (r.Status == "OVER_QUERY_LIMIT") { _location.SearchResult = (int)LocationSearchResultEnum.OverProcessCountLimit; } else { _location.SearchResult = (int)LocationSearchResultEnum.Error; } return(_location); }
public GoogleSearchAddressResponse GetLocation(Address address) { string strAddress = BuildAddressString(address); GoogleSearchAddressResponse location = new GoogleSearchAddressResponse(); List <Address> points = new List <Address>(); GeoResponse r = GetGoogleLocation(strAddress); if (r != null) { if ((r.Status == "OK")) { location.SearchResult = (int)LocationSearchResultEnum.Found; var v = r.Results.ToList(); foreach (var rr in v) { Address s_addr = new Address(); GeoResponse.CResult.CGeometry.CLocation loc = rr.Geometry.Location; s_addr.Latitude = loc.Lat; s_addr.Longitude = loc.Lng; s_addr.FullAddressString = rr.formatted_address; s_addr.LocationType = rr.Geometry.LocationType; string sNum = "", sStreet = "", sCity = "", sState = ""; GeoResponse.CResult.GeocodeAddressComponent[] addrc = rr.AddressComponents; List <GoogleAddressPartsToDB> addrDbProps = GetCountryGoogleProps(address.CountryId); if ((addrDbProps != null) && (addrDbProps.Count > 0)) { List <GeoResponse.CResult.GeocodeAddressComponent> responseComponents = addrc.ToList(); sState = GetAddressPartString(2, responseComponents, addrDbProps); sCity = GetAddressPartString(3, responseComponents, addrDbProps); sStreet = GetAddressPartString(4, responseComponents, addrDbProps); sNum = GetAddressPartString(5, responseComponents, addrDbProps); s_addr.Building = sNum; s_addr.Street = sStreet; s_addr.City = sCity; s_addr.State = sState; s_addr.Country = address.Country; } else { for (int i = 0; i < addrc.Length; i++) { string sprop = addrc[i].Type[0]; if (sprop == "street_number") { sNum = addrc[i].ShortName; } if (sprop == "route") { sStreet = addrc[i].ShortName; } if (sprop == "administrative_area_level_1") { sState = addrc[i].ShortName; } if (sprop == "locality") { sCity = addrc[i].ShortName; } } } s_addr.Building = sNum; s_addr.Street = sStreet; s_addr.City = sCity; s_addr.State = sState; s_addr.Country = address.Country; points.Add(s_addr); } } else if (r.Status == "ZERO_RESULTS") { location.SearchResult = (int)LocationSearchResultEnum.NotFound; } else if (r.Status == "REQUEST_DENIED") { location.SearchResult = (int)LocationSearchResultEnum.RequestDenied; } else if (r.Status == "OVER_QUERY_LIMIT") { location.SearchResult = (int)LocationSearchResultEnum.OverProcessCountLimit; } else { location.SearchResult = (int)LocationSearchResultEnum.Error; } } location.ServiceResponseAddresses = points; return(location); }