Question
I am working on a project that retrieves addresses from Google's API. I am getting response from google, but I am having trouble displaying the
I am working on a project that retrieves addresses from Google's API. I am getting response from google, but I am having trouble displaying the address results(AddressList) to the radgridview. I am using a WPF application and just need to display the results to the RADGRIDVIEW. I am using 'ItemSource' to try to bind to the grid but I am getting the following error: Invalid Cross Thread Access.
CODE
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using MR.CommandBridge.Common.View; using MR.CommandBridge.CES; using MR.CommandBridge.Common.Controls; using MR.CommandBridge.Common.EntityTypes; using MR.CommandBridge.Common; using System.IO; using System.Text.RegularExpressions; using Newtonsoft.Json.Linq; using System.Collections.ObjectModel; namespace MR.CommandBridge.VEMap.ContentApps { public partial class AddressSearch : ContentAppEntityView { public AddressSearch() { InitializeComponent(); }
HttpWebRequest request = null;
#region **********SEARCHING**********
private void addressInput_GotFocus(object sender, RoutedEventArgs e) { if (addressInput.Text == "Search") addressInput.Text = ""; else addressInput.SelectAll(); }
private void addressInput_LostFocus(object sender, RoutedEventArgs e) { if (addressInput.Text == String.Empty) { addressInput.Text = "Search"; searchclose.Opacity = 0; } else if (addressInput.Text == "Search") searchclose.Opacity = 0; }
private void EmptySearch_Click(object sender, RoutedEventArgs e) { addressInput.Text = "Search"; searchclose.Opacity = 0; }
private void addressInput_KeyDown(object sender, KeyEventArgs e) { searchclose.Opacity = 1; }
private void Enter_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { var address = addressInput.Text;
var requestUri = string.Format("http://localhost/media/TextToSpeech/TextToSpeechService.svc/getDataProxy?url=https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=AIzaSyCgsNpuUoH7m6U7lqeZjlLZ3MgM15PW15o", Uri.EscapeDataString(address)); //var requestUri = string.Format(ConfigurationManager.GetAppSetting("addressSearchGeoCode", Uri.EscapeDataString(address)));
HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest; request.Method = "GET"; IAsyncResult result = request.BeginGetResponse(new AsyncCallback(RequestCompleted), request); var dodo = result; } }
private void RequestCompleted(IAsyncResult result) { var request = (HttpWebRequest)result.AsyncState; var response = (HttpWebResponse)request.EndGetResponse(result); Stream stream = response.GetResponseStream();
try { StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd();
Regex rgx = new Regex("<.*\\>"); string newResult = rgx.Replace(text, "");
JObject json = JObject.Parse(newResult); JArray results = (JArray)json["results"];
List latList = new List(); List lngList = new List(); List AddressList = new List();
if (results.Count == 0) { MessageBox.Show("No results found"); } else { foreach (JObject obj in results) { if (obj == null) { MessageBox.Show("Address returned no results"); } string formattedAddress = (string)obj["formatted_address"]; AddressList.Add(formattedAddress);
double lat = (double)obj["geometry"]["location"]["lat"]; latList.Add(lat);
double lng = (double)obj["geometry"]["location"]["lng"]; lngList.Add(lng); //TODO Add exception handling } this.addressGrid.ItemsSource = AddressList; }
} catch (Exception ex) { MessageBox.Show("Error" + ex.Message); }
}
}
}
Section of XAML
GotFocus="addressInput_GotFocus" LostFocus="addressInput_LostFocus" Text="{Binding Path=StringLibrary.Search, Source={StaticResource Strings}}" Background="Transparent" FontFamily="Segoe UI" FontSize="13" VerticalAlignment="Center" Foreground="Gray" KeyDown="Enter_KeyDown" BorderBrush="Transparent" BorderThickness="0" Style="{StaticResource TextBoxStyle1}"> CommandParameter="{Binding Text, ElementName=SearchTB}"/> CommandParameter="{Binding Text, ElementName=SearchTB}"/>
ItemsSource="{Binding}" CanUserFreezeColumns="False" CanUserSelect="False" EnableColumnVirtualization="True" EnableRowVirtualization="True" ShowGroupPanel="False" ScrollViewer.VerticalScrollBarVisibility="Visible" CanUserDeleteRows="True" RowIndicatorVisibility="Collapsed" DataLoadMode="Asynchronous" HeaderRowStyle="{StaticResource ZoneActivityHeaderRowStyle}" GroupPanelStyle="{StaticResource GroupPanelStyle}" Background="#00000000" VerticalGridLinesBrush="#00000000" HorizontalGridLinesBrush="#00000000" AlternationCount="2" RowHeight="32" ShowColumnHeaders="False" RowStyle="{StaticResource ActivityRowStyle}" AlternateRowStyle="{StaticResource ActivityAlternateRowStyle}">
IsFilterable="False" ShowDistinctFilters="True" IsGroupable="False"> FontSize="12" VerticalAlignment="Top" Margin="5" TextWrapping="Wrap" />
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started