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 Map_Tapped_1(object sender, TappedRoutedEventArgs e) { var mp = MyMap.ScreenToMap(e.GetPosition(MyMap)); Graphic g = new Graphic() { Geometry = mp }; var layer = MyMap.Layers.OfType<GraphicsLayer>().First(); layer.Graphics.Add(g); ESRI.ArcGIS.Runtime.Tasks.Locator locatorTask = new ESRI.ArcGIS.Runtime.Tasks.Locator( new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer", UriKind.Absolute)); LocationToAddressParameter parameter = new LocationToAddressParameter(); // Tolerance (distance) specified in meters parameter.Distance = 30; parameter.Location = mp; parameter.OutSpatialReference = MyMap.SpatialReference; try { LocationToAddressResult result = await locatorTask.LocationToAddressAsync(parameter, CancellationToken.None); Address address = result.Address; IDictionary<string, object> attributes = address.Attributes; Graphic graphic = new Graphic() { Geometry = mp}; string latlon = String.Format("{0}, {1}", address.Location.X, address.Location.Y); string address1 = attributes["Street"].ToString(); string address2 = String.Format("{0}, {1} {2}", attributes["City"], attributes["State"], attributes["ZIP"]); graphic.Attributes.Add("LatLon", latlon); graphic.Attributes.Add("Address1", address1); graphic.Attributes.Add("Address2", address2); _locationGraphicsLayer.Graphics.Add(graphic); } catch(Exception ex) { return; } }
public BatchGeocoding() { this.InitializeComponent(); MyMap.InitialExtent = new Envelope(-15000000, 2000000, -7000000, 8000000) { SpatialReference = new SpatialReference(102100) }; _locatorTask = new ESRI.ArcGIS.Runtime.Tasks.Locator (new Uri("http://serverapps101.esri.com/arcgis/rest/services/USA_Geocode/GeocodeServer",UriKind.Absolute)); geocodedResults = MyMap.Layers["LocationGraphicsLayer"] as GraphicsLayer; //List of addresses to geocode batchaddresses.Add(new Dictionary<string, string> { { "Street", "4409 Redwood Dr" }, { "Zip", "92501" } }); batchaddresses.Add(new Dictionary<string, string> { { "Street", "3758 Cedar St" }, { "Zip", "92501" } }); batchaddresses.Add(new Dictionary<string, string> { { "Street", "3486 Orange St" }, { "Zip", "92501" } }); batchaddresses.Add(new Dictionary<string, string> { { "Street", "2999 4th St" }, { "Zip", "92507" } }); batchaddresses.Add(new Dictionary<string, string> { { "Street", "3685 10th St" }, { "Zip", "92501" } }); AddressListbox.ItemsSource = batchaddresses; }
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); } }