async void Handle_Clicked(object sender, System.EventArgs e) { try { if (!String.IsNullOrEmpty(xSearch.Text)) { xload.IsVisible = true; PleaceAutoComplete pleaceAutoComplete = await BaseHTTPService.GetPlaceAutocomplete(xSearch.Text); List <string> ListCidades = new List <string>(); foreach (var cidade in pleaceAutoComplete.Predictions) { ListCidades.Add(cidade.Description); } ListAddress.ItemsSource = ListCidades; } } catch (Exception ex) { Debug.WriteLine("Error:" + ex.Message); } finally{ xload.IsVisible = false; } }
async void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e) { try { List <string> ListCidades = new List <string>(); xload.IsVisible = true; if (e.NewTextValue.Length > 2) { PleaceAutoComplete pleaceAutoComplete = await BaseHTTPService.GetPlaceAutocomplete(e.NewTextValue); foreach (var cidade in pleaceAutoComplete.Predictions) { ListCidades.Add(cidade.Description); } ListAddress.ItemsSource = ListCidades; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally{ xload.IsVisible = false; } }
public static async Task <PleaceAutoComplete> GetPlaceAutocomplete(string cidade) { PleaceAutoComplete result = new PleaceAutoComplete(); HttpResponseMessage response = await HttpUtil.GetAsync("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + cidade + "&types=(cities)&language=pt_BR&key=" + keyGoogleAPI); switch ((int)response.StatusCode) { case 200: result = JsonConvert.DeserializeObject <PleaceAutoComplete>(response.Content.ReadAsStringAsync().Result); break; case 400: var obj = JsonConvert.DeserializeObject <object>(response.Content.ReadAsStringAsync().Result); break; default: result = null; break; } return(result); }