Locator.xaml.cs
上传用户:huazai0421
上传日期:2008-05-30
资源大小:405k
文件大小:5k
源码类别:

SilverLight

开发平台:

C#

  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using ESRI.ArcGIS.Client.Geometry;
  8. using ESRI.ArcGIS.Client.Tasks;
  9. namespace ESRI.ArcGIS.Samples
  10. {
  11. public partial class LocatorControl : UserControl
  12. {
  13. public LocatorControl()
  14. {
  15. InitializeComponent();
  16. }
  17. #region Search Location
  18. private void TextBox_GotFocus(object sender, RoutedEventArgs e)
  19. {
  20. TextBox tb = (sender as TextBox);
  21. tb.GotFocus -= TextBox_GotFocus;
  22. tb.Text = "";
  23. tb.Foreground = new SolidColorBrush(Colors.White);
  24. }
  25. private void TextBox_LostFocus(object sender, RoutedEventArgs e)
  26. {
  27. TextBox tb = (sender as TextBox);
  28. if (string.IsNullOrEmpty(tb.Text))
  29. {
  30. tb.Text = "Enter a location...";
  31. tb.Foreground = new SolidColorBrush(Colors.Gray);
  32. tb.GotFocus += TextBox_GotFocus;
  33. }
  34. }
  35. private void SearchLocation_KeyDown(object sender, KeyEventArgs e)
  36. {
  37. if (e.Key != Key.Enter) return;
  38. TextBox tb = sender as TextBox;
  39. Locator locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Places_World/GeocodeServer");
  40. locatorTask.AddressToLocationsCompleted += LocatorTask_AddressToLocationsCompleted;
  41. //locatorTask.Failed += LocatorTask_Failed;
  42. AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
  43. Dictionary<string, string> address = addressParams.Address;
  44. addressParams.OutFields.Add("North_Lat");
  45. addressParams.OutFields.Add("South_Lat");
  46. addressParams.OutFields.Add("West_Lon");
  47. addressParams.OutFields.Add("East_Lon");
  48. addressParams.OutFields.Add("Descr");
  49. if (!string.IsNullOrEmpty(tb.Text))
  50. address.Add("PlaceName", tb.Text);
  51. else return;
  52. locatorTask.AddressToLocationsAsync(addressParams);
  53. }
  54. private void LocatorTask_AddressToLocationsCompleted(object sender, AddressToLocationsEventArgs args)
  55. {
  56. List<AddressCandidate> returnedCandidates = args.Results;
  57. if (returnedCandidates.Count == 1)
  58. {
  59. AddressCandidate best = returnedCandidates[0];
  60. Envelope env = new Envelope()
  61. {
  62. XMin = double.Parse(best.Attributes["West_Lon"].ToString(), CultureInfo.InvariantCulture),
  63. YMin = double.Parse(best.Attributes["South_Lat"].ToString(), CultureInfo.InvariantCulture),
  64. XMax = double.Parse(best.Attributes["East_Lon"].ToString(), CultureInfo.InvariantCulture),
  65. YMax = double.Parse(best.Attributes["North_Lat"].ToString(), CultureInfo.InvariantCulture)
  66. };
  67. Map.ZoomTo(env);
  68. }
  69. else if (returnedCandidates.Count > 1)
  70. {
  71. locationResults.Visibility = Visibility.Visible;
  72. locationResults.ItemsSource = returnedCandidates;
  73. Dispatcher.BeginInvoke(() =>
  74. {
  75. locationResults.Focus();
  76. });
  77. }
  78. }
  79. private void locationResults_LostFocus(object sender, RoutedEventArgs e)
  80. {
  81. locationResults.Visibility = Visibility.Collapsed;
  82. locationResults.ItemsSource = null;
  83. }
  84. private void locationResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
  85. {
  86. AddressCandidate best = locationResults.SelectedItem as AddressCandidate;
  87. if (best == null) return;
  88. Envelope env = new Envelope()
  89. {
  90. XMin = double.Parse(best.Attributes["West_Lon"].ToString(), CultureInfo.InvariantCulture),
  91. YMin = double.Parse(best.Attributes["South_Lat"].ToString(), CultureInfo.InvariantCulture),
  92. XMax = double.Parse(best.Attributes["East_Lon"].ToString(), CultureInfo.InvariantCulture),
  93. YMax = double.Parse(best.Attributes["North_Lat"].ToString(), CultureInfo.InvariantCulture)
  94. };
  95. Map.ZoomTo(env);
  96. locationResults.Visibility = Visibility.Collapsed;
  97. locationResults.ItemsSource = null;
  98. }
  99. private void locationResults_SizeChanged(object sender, SizeChangedEventArgs e)
  100. {
  101. if (e.NewSize.Width > 0)
  102. {
  103. RearrangeLocationDropdown(e.NewSize);
  104. }
  105. }
  106. private void RearrangeLocationDropdown(Size size)
  107. {
  108. double offset = 0;
  109. //Get placement of popup so it pops up under textbox
  110. Point p = locationTextBox.TransformToVisual(locationResultsPopUp.Parent as UIElement).Transform(new Point(0, locationTextBox.ActualHeight));
  111. //If dropdown gets clipped to the right, move it a bit to the left
  112. Point p2 = locationResultsPopUp.TransformToVisual(Application.Current.RootVisual).Transform(new Point(size.Width+p.X, 0));
  113. if ((Application.Current.RootVisual as FrameworkElement).ActualWidth < p2.X)
  114. offset = (Application.Current.RootVisual as FrameworkElement).ActualWidth - p2.X;
  115. locationResultsPopUp.HorizontalOffset = p.X + offset;
  116. locationResultsPopUp.VerticalOffset = p.Y;
  117. }
  118. #endregion Search Location
  119. public ESRI.ArcGIS.Client.Map Map
  120. {
  121. get { return (ESRI.ArcGIS.Client.Map)GetValue(MapProperty); }
  122. set { SetValue(MapProperty, value); }
  123. }
  124. public static readonly DependencyProperty MapProperty =
  125. DependencyProperty.Register("Map", typeof(ESRI.ArcGIS.Client.Map), typeof(LocatorControl), null);
  126. }
  127. }