private async void FindAddressButton_Click(object sender, RoutedEventArgs e) { _locatorTask = new ESRI.ArcGIS.Runtime.Tasks.Locator (new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer", UriKind.Absolute)); AddressToLocationsParameter addressParams = new AddressToLocationsParameter() { OutSpatialReference = MyMap.SpatialReference }; IDictionary<string, string> address = addressParams.Address; if (!string.IsNullOrEmpty(InputAddress.Text)) address.Add("Street", InputAddress.Text); if (!string.IsNullOrEmpty(City.Text)) address.Add("City", City.Text); if (!string.IsNullOrEmpty(State.Text)) address.Add("State", State.Text); if (!string.IsNullOrEmpty(Zip.Text)) address.Add("ZIP", Zip.Text); try { AddressToLocationsResult result = await _locatorTask.AddressToLocationsAsync(addressParams); if (_candidateGraphicsLayer.Graphics != null && _candidateGraphicsLayer.Graphics.Count > 0) _candidateGraphicsLayer.Graphics.Clear(); if (result != null) { foreach (AddressCandidate candidate in result.AddressCandidates) { if (candidate.Score >= 80) { Graphic graphic = new Graphic() { Geometry = candidate.Location }; graphic.Attributes.Add("Address", candidate.Address); string latlon = String.Format("{0}, {1}", candidate.Location.X, candidate.Location.Y); graphic.Attributes.Add("LatLon", latlon); if (candidate.Location.SpatialReference == null) { candidate.Location.SpatialReference = SpatialReference.Wgs84; } _candidateGraphicsLayer.Graphics.Add(graphic); } } } } catch (Exception ex) { return; } }
private async void GetDirections_Click(object sender, RoutedEventArgs e) { //Reset DirectionsStackPanel.Children.Clear(); var _stops = new List<Graphic>(); var _locator = new ESRI.ArcGIS.Runtime.Tasks.Locator(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Address_NA/GeocodeServer")); var routeLayer = MyMap.Layers["MyRouteGraphicsLayer"] as GraphicsLayer; routeLayer.Graphics.Clear(); try { //Geocode from address var fromLocation = await _locator.AddressToLocationsAsync(ParseAddress(FromTextBox.Text)); if (fromLocation.AddressCandidates != null && fromLocation.AddressCandidates.Count > 0) { AddressCandidate address = fromLocation.AddressCandidates.FirstOrDefault(); Graphic graphicLocation = new Graphic() { Geometry = address.Location, Symbol = LayoutRoot.Resources["FromSymbol"] as ISymbol }; graphicLocation.Attributes["address"] = address.Address; graphicLocation.Attributes["score"] = address.Score; _stops.Add(graphicLocation); routeLayer.Graphics.Add(graphicLocation); } //Geocode to address var toLocation = await _locator.AddressToLocationsAsync(ParseAddress(ToTextBox.Text)); if (toLocation.AddressCandidates != null && toLocation.AddressCandidates.Count > 0) { AddressCandidate address = toLocation.AddressCandidates.FirstOrDefault(); Graphic graphicLocation = new Graphic() { Geometry = address.Location, Symbol = LayoutRoot.Resources["ToSymbol"] as ISymbol }; graphicLocation.Attributes["address"] = address.Address; graphicLocation.Attributes["score"] = address.Score; _stops.Add(graphicLocation); routeLayer.Graphics.Add(graphicLocation); } //Get route between from and to var _routeParams = new RouteParameter() { ReturnRoutes = false, ReturnDirections = true, DirectionsLengthUnits = MapUnit.Miles, Stops = new FeatureStops(_stops), UseTimeWindows = false }; var _routeTask = new RouteTask(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route")); _routeParams.OutSpatialReference = MyMap.SpatialReference; var routeTaskResult = await _routeTask.SolveAsync(_routeParams); _directionsFeatureSet = routeTaskResult.Directions.FirstOrDefault(); routeLayer.Graphics.Add(new Graphic() { Geometry = _directionsFeatureSet.MergedGeometry, Symbol = LayoutRoot.Resources["RouteSymbol"] as ISymbol }); TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(_directionsFeatureSet.RouteSummary.TotalLength, "miles")); TotalTimeTextBlock.Text = string.Format("Total Time: {0}", FormatTime(_directionsFeatureSet.RouteSummary.TotalTime)); TitleTextBlock.Text = _directionsFeatureSet.RouteName; int i = 1; foreach (Graphic graphic in _directionsFeatureSet.Graphics) { System.Text.StringBuilder text = new System.Text.StringBuilder(); text.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]); if (i > 1 && i < _directionsFeatureSet.Graphics.Count) { string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles"); string time = null; if (graphic.Attributes.ContainsKey("time")) { time = FormatTime(Convert.ToDouble(graphic.Attributes["time"])); } if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time)) text.Append(" ("); text.Append(distance); if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time)) text.Append(", "); text.Append(time); if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time)) text.Append(")"); } TextBlock textBlock = new TextBlock() { Text = text.ToString(), Tag = graphic, Margin = new Thickness(4) }; textBlock.Tapped += TextBlock_Tapped; DirectionsStackPanel.Children.Add(textBlock); i++; } MyMap.ZoomTo(_directionsFeatureSet.RouteSummary.Extent.Expand(0.6)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }