public void GeocodeAddress(string address, string city, string state, string zipCode, string country) { if (locatorConfig.EnableLocator == ServiceSource.BING) { ESRI.ArcGIS.Client.Bing.Geocoder bingLocator = new ESRI.ArcGIS.Client.Bing.Geocoder(locatorConfig.BingLocator.Token, locatorConfig.BingLocator.ServerType); address = (string.IsNullOrEmpty(address)) ? "" : address + ","; city = (string.IsNullOrEmpty(city)) ? "" : city + ","; string formatAddress = string.Format("{0} {1} {2} {3}", address, city, state, zipCode, country); if (!string.IsNullOrEmpty(country)) { formatAddress += "," + country; } bingLocator.Geocode(formatAddress, new EventHandler <ESRI.ArcGIS.Client.Bing.GeocodeService.GeocodeCompletedEventArgs>(BingLocator_GeocodeCompleted)); } else if (locatorConfig.ArcGISLocator != null) { ESRI.ArcGIS.Client.Tasks.Locator locatorTask = new ESRI.ArcGIS.Client.Tasks.Locator(locatorConfig.ArcGISLocator.RESTURL); locatorTask.AddressToLocationsCompleted += new EventHandler <ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs>(ArcGISLocator_GeocodeCompleted); locatorTask.Failed += new EventHandler <TaskFailedEventArgs>(ArcGISLocatorTask_Failed); AddressToLocationsParameters addressParams = new AddressToLocationsParameters(); ArcGISLocatorParams paramFields = locatorConfig.ArcGISLocator.ParameterFields; if (!string.IsNullOrEmpty(paramFields.AddressField)) { addressParams.Address.Add(paramFields.AddressField, address); } if (!string.IsNullOrEmpty(paramFields.CityField)) { addressParams.Address.Add(paramFields.CityField, city); } if (!string.IsNullOrEmpty(paramFields.StateField)) { addressParams.Address.Add(paramFields.StateField, state); } if (!string.IsNullOrEmpty(paramFields.ZipField)) { addressParams.Address.Add(paramFields.ZipField, zipCode); } if (!string.IsNullOrEmpty(paramFields.CountryField)) { addressParams.Address.Add(paramFields.CountryField, country); } locatorTask.AddressToLocationsAsync(addressParams); } }
private void ArcGISLocator_ReverseGeocodeCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressEventArgs args) { List <LocationCandidate> results = new List <LocationCandidate>(); if (args.Address != null) { string value = ""; string formatAddress = ""; Dictionary <string, object> values = args.Address.Attributes; ArcGISLocatorParams fields = locatorConfig.ArcGISLocator.ParameterFields; if (fields.AddressField != null && values.ContainsKey(fields.AddressField)) { value = (string)values[fields.AddressField]; if (!string.IsNullOrEmpty(value)) { formatAddress += value.Trim() + ", "; } } if (fields.CityField != null && values.ContainsKey(fields.CityField)) { value = (string)values[fields.CityField]; if (!string.IsNullOrEmpty(value)) { formatAddress += value.Trim(); } } if (fields.StateField != null && values.ContainsKey(fields.StateField)) { value = (string)values[fields.StateField]; if (!string.IsNullOrEmpty(value)) { formatAddress += ", " + value.Trim() + " "; } } if (fields.ZipField != null && values.ContainsKey(fields.ZipField)) { value = (string)values[fields.ZipField]; if (!string.IsNullOrEmpty(value)) { formatAddress += value.Trim(); } } if (fields.CountryField != null && values.ContainsKey(fields.CountryField)) { value = (string)values[fields.CountryField]; if (!string.IsNullOrEmpty(value)) { formatAddress += ", " + value.Trim(); } } if (formatAddress == "") { formatAddress = "Address is unavailable"; } LocationCandidate candidate = new LocationCandidate(100, formatAddress, args.Address.Location, locatorConfig.LocationSymbol.ImageSource); results.Add(candidate); } if (results.Count > 0) { int srWKID = results[0].Location.SpatialReference.WKID; OnResultReady(results, srWKID, true); // With ArcGIS Server 10.01 later, reverse geocoding results have the same SR with the input point } else if (ResultReady != null) { ResultReady(this, new GeocodeResultEventArgs(results, true)); } }