/// <summary> /// Do a search for the contents of the specified URL /// </summary> /// <param name="text"></param> /// <param name="numResults"></param> /// <returns></returns> public async static Task <CandidateResponse> SearchFor(string text, int numResults = 2) { //WebClient wc = new WebClient(); HttpClient hc = new HttpClient(); hc.DefaultRequestHeaders.Add("user-agent", "GeocodeExample"); hc.DefaultRequestHeaders.Add("referer", "GeocodeExample"); //wc.Headers.Add("user-agent", "GeocodeExample"); //wc.Headers.Add("referer", "GeocodeExample"); //wc.Encoding = System.Text.Encoding.UTF8; CandidateResponse geocodeResult = null; var uri = new Geocode.GeocodeURI(text, numResults).Uri; using (var httpResponse = await hc.GetStreamAsync(uri).ConfigureAwait(false)) { using (StreamReader sr = new StreamReader(httpResponse, System.Text.Encoding.UTF8, true)) { string response = sr.ReadToEnd(); if (ResponseIsError(response)) { //throw throw new System.ApplicationException(response); } geocodeResult = ObjectSerialization.JsonToObject <CandidateResponse>(response); } } return(geocodeResult); }
private async void Button_Click(object sender, RoutedEventArgs e) { // get the current module to access helper method GeocodeModule module = GeocodeModule.Current; // remove any existing graphics MapView mapView = MapView.Active; GeocodeUtils.RemoveFromMapOverlay(mapView); try { // initiate the search CandidateResponse results = await GeocodeUtils.SearchFor(this.SearchText.Text, 1); if (results.OrderedResults.Count > 0) { // add a point graphic overlay GeocodeUtils.UpdateMapOverlay(await results.OrderedResults[0].ToMapPoint(), mapView); // zoom to the location await GeocodeUtils.ZoomToLocation(results.OrderedResults[0].Extent); // add the search results to the dialog window this.LastSearch.Text = string.Format("Last Match: {0}", results.OrderedResults[0].CandidateDetails); } else { ArcGIS.Desktop.Internal.Framework.DialogManager.ShowMessageBox( string.Format("No results returned for {0}", this.SearchText.Text), "GeocodeExample"); } } catch (Exception ex) { string errorName = "Search Error"; System.Diagnostics.Trace.WriteLine(string.Format("{0}: {1}", errorName, ex.ToString())); ArcGIS.Desktop.Internal.Framework.DialogManager.ShowMessageBox(errorName, this.SearchText.Text); } }
/// <summary> /// Do a search for the contents of the specified URL /// </summary> /// <param name="url"></param> /// <returns></returns> public static CandidateResponse SearchFor(string text, int numResults = 2) { WebClient wc = new WebClient(); wc.Headers.Add("user-agent", "GeocodeExample"); wc.Headers.Add("referer", "GeocodeExample"); wc.Encoding = System.Text.Encoding.UTF8; CandidateResponse geocodeResult = null; using (StreamReader sr = new StreamReader(wc.OpenRead(new Geocode.GeocodeURI(text, numResults).Uri), System.Text.Encoding.UTF8, true)) { string response = sr.ReadToEnd(); if (ResponseIsError(response)) { //throw throw new System.ApplicationException(response); } geocodeResult = ObjectSerialization.JsonToObject <CandidateResponse>(response); } return(geocodeResult); }