Ejemplo n.º 1
0
        /// <summary>
        /// Invoked when user click on search button, and this event handler will consume Google Places Api,
        /// and then populate view model
        /// </summary>
        /// <param name="sender">The object sender</param>
        /// <param name="e">The events args</param>
        private async void SearchBarSearchTerm_SearchButtonPressed(object sender, EventArgs e)
        {
            var searchBar = sender as SearchBar;

            if (!string.IsNullOrWhiteSpace(searchBar?.Text))
            {
                var result = await DownloadHelper.DownloadStringAsync(StrAutoCompleteGoogleApi + searchBar.Text + "&key=" + StrGoogleApiKey);

                _viewModel.GoogleMapPlace = JsonConvert.DeserializeObject <GoogleMapPlace>(result);

                if (_viewModel.GoogleMapPlace.Status != "OK")
                {
                    await DisplayAlert("Invalid Location", "Something wrong with your search term.\nPlease try to enter a valid location!!!", "Ok");

                    return;
                }

                // Because I am stucked in building an autocomplete control, I decided to get the first address.
                var addressUrl = StrGeoCodingUrl + _viewModel.GoogleMapPlace.Predictions[0].Description;
                var addressGeo = await DownloadHelper.DownloadStringAsync(addressUrl);

                _viewModel.GeoCode = JsonConvert.DeserializeObject <GeoCode>(addressGeo);

                var lat = _viewModel.GeoCode.Results[0].Geometry.Location.Lat;
                var lng = _viewModel.GeoCode.Results[0].Geometry.Location.Lng;

                try
                {
                    await _viewModel.InitTweetsByGeoLocation(lat, lng);
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Error", "Something goes wrong while populating the view model!!!", "Ok");

                    Debug.WriteLine(ex.Message);
                }
            }
        }