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 AddressToLocationsParameter ParseAddress(string address) { string[] fromArray = address.Split(new char[] { ',' }); AddressToLocationsParameter fromAddress = new AddressToLocationsParameter(); fromAddress.OutFields.Add("Loc_name"); fromAddress.Address.Add("Address", fromArray[0]); fromAddress.Address.Add("City", fromArray[1]); fromAddress.Address.Add("State", fromArray[2]); fromAddress.Address.Add("Zip", fromArray[3]); fromAddress.Address.Add("Country", "USA"); return fromAddress; }