Potrzebuję pobrać kod pocztowy z odpowiedzi Google.
public PostCodeResponse GetAddressAndPostCodeByLatLongList(decimal lat, decimal lng)
{
var myObject = GetGoogleJsonObject(lat, lng);
if (myObject.status == "OK")
{
if (myObject.results[0].formatted_address == "Polska")
{
return new PostCodeResponse() { Name = "", Status = ApiStatus.Success };
//return "";
}
var licznik = myObject.results[0].address_components.Count - 1;
const string sPattern = (@"^[0-9]{2}\-[0-9]{3}$");
Regex regex = new Regex(@"^[0-9]{2}\-[0-9]{3}$");
var q =
myObject.results.Select(s => s.address_components.Select(d => d.long_name)).ToList();
var formatedAdres = myObject.results.Where(r => Regex.IsMatch(r.formatted_address, @"[0-9]{2}\-[0-9]{3}")).Select(s => s.formatted_address).ToList();
string[] mached = null;
foreach (var formatedAdre in formatedAdres)
{
mached = Regex.Matches(formatedAdre, @"[0-9]{2}\-[0-9]{3}").Cast<Match>().Select(m => m.Value).ToArray();
}
var st = Regex.Split(mached.First(), @"[0-9]{2}\-[0-9]{3}");
dynamic temp = null;
// dynamic temp = formatedAdres.Where(t => Regex.IsMatch(t, @"[0-9]{2}\-[0-9]{3}")).Aggregate<string, dynamic>(null, (current, t) => current + t);
for (int index = 0; index < formatedAdres.Count; index++)
{
if (Regex.IsMatch(formatedAdres[index], @"[0-9]{2}\-[0-9]{3}")) //OK
{
temp += formatedAdres[index];
}
}
var fullAdress = myObject.results[0].address_components[myObject.results[0].address_components.Count - 5].long_name;
var postCode = myObject.results[0].address_components[myObject.results[0].address_components.Count - 1].long_name;
postCode = postCode.Contains("Poland") ? "" : postCode;
return new PostCodeResponse() { Name = fullAdress, PostCode = postCode, Status = ApiStatus.Success , FarmatedAdres = "" };
//return fullAdress;
}
Otrzymuję w odpowiedzi listę (czasem 9 itemów) [myObject]
Z tej listy potrzebuję pobrać tylko pierwsze wystąpienie spełniające warunek:
var postCode = myObject.results[0].address_components[myObject.results[0].address_components.Count - 1].long_name;
W linijce powyżej ma na sztywno ustawione, że to pierwszy element ale tak nie zawsze jest.
Potrzebuję np. przeszukać tę listę obiektów wyrażeniem linq lub inaczej i wybrać element spełniający warunek z wyrażenia regularnego.
Kod odczytujący odpowiedź z Google:
private string GetGoogleJson(decimal lat, decimal lng)
{
var culture = CultureInfo.InvariantCulture;
var request = WebRequest.Create(string.Format("http://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&sensor=false",
lat.ToString(culture), lng.ToString(culture))) as HttpWebRequest;
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
var responseContent = reader.ReadToEnd().Trim();
return responseContent.ToString();
}
}
return null;
}
Zapytanie do Google: "http://maps.googleapis.com/maps/api/geocode/json?latlng=51.68,17.13&sensor=false"
List obiektów do mapowania do uzyskania pod adresem: http://json2csharp.com po klejeniu odpowiedzi JSON
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Northeast2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
public Bounds bounds { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> types { get; set; }
public List<string> postcode_localities { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
Dzięki