Jamal: rest of example codeCode behind: using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Input; using ESRI.ArcGIS.Client.Extensibility; using ESRI.ArcGIS.Client.Tasks; namespace MyAddressLocationTool { public class AddressLocationViewModel : INotifyPropertyChanged { private AddressLocationConfigData data = null; public AddressLocationViewModel(Map map, AddressLocationConfigData data) { this.Map = map; this.data = data; ValidateConfigData(data); InputAddress = new Address(); AddressToLocation = new AddressToLocationCommand(); AddressToLocation.LocatorURL = data.AddressLocatorURL; AddressToLocation.Completed += AddressToLocation_Completed; AddressToLocation.Failed += AddressToLocation_Failed; } public Map Map { get; private set; } public Address InputAddress { get; private set; } public AddressToLocationCommand AddressToLocation { get; private set; } private void ValidateConfigData(AddressLocationConfigData addressLocationConfigData) { Exception ex = null; if (addressLocationConfigData == null) { MainApplication.Handle("Search Configuration Error", "AddressLocation configuration is null"); throw ex; } else if (string.IsNullOrWhiteSpace(addressLocationConfigData.AddressLocatorURL)) { MainApplication.Handle("Search Configuration Error", "Address Locator URL is empty"); throw ex; } } private string error; public string Error { get { return error; } private set { if (error != value) { error = value; OnPropertyChanged("Error"); } } } private List<AddressCandidate> matchCandidates; public List<AddressCandidate> MatchCandidates { get { return matchCandidates; } set { if (matchCandidates == null || !matchCandidates.Equals(value)) { matchCandidates = value; OnPropertyChanged("MatchCandidates"); } } } private void AddressToLocation_Completed(object sender, AddressToLocationsEventArgs e) { MatchCandidates = e.Results; } private void AddressToLocation_Failed(object sender, TaskFailedEventArgs e) { Error = e.Error.Message; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class Address : INotifyPropertyChanged { private string _singleline; public string SingleLine { get { return _singleline; } set { if (_singleline != value) _singleline = value; OnPropertyChanged("SingleLine"); } } //private string _street; //public string Street //{ // get // { // return _street; // } // set // { // if (_street != value) // _street = value; // OnPropertyChanged("Street"); // } //} //private string _city; //public string City //{ // get // { // return _city; // } // set // { // if (_city != value) // _city = value; // OnPropertyChanged("City"); // } //} //private string _state; //public string State //{ // get // { // return _state; // } // set // { // if (_state != value) // _state = value; // OnPropertyChanged("State"); // } //} //private string _zipCode; //public string ZipCode //{ // get // { // return _zipCode; // } // set // { // if (_zipCode != value) // _zipCode = value; // OnPropertyChanged("ZipCode"); // } //} public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class AddressToLocationCommand : ICommand { private Address address; private string locatorURL = string.Empty; public string LocatorURL { get { return locatorURL; } set { locatorURL = value; } } public bool CanExecute(object parameter) { Address newAddress = parameter as Address; if (address == null || !address.Equals(newAddress)) { if (address != null) address.PropertyChanged -= Address_PropertyChanged; address = newAddress; if (address != null) address.PropertyChanged += Address_PropertyChanged; } if (address == null) return false; return !string.IsNullOrEmpty(address.SingleLine); //return !string.IsNullOrEmpty(address.Street) && !string.IsNullOrEmpty(address.City) // && !string.IsNullOrEmpty(address.State) && !string.IsNullOrEmpty(address.ZipCode); } private void Address_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty); } public event EventHandler CanExecuteChanged; public void Execute(Object parameter) { //Locator locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US/GeocodeServer"); Locator locatorTask = new Locator(locatorURL); locatorTask.AddressToLocationsCompleted += new EventHandler<AddressToLocationsEventArgs>(locatorTask_AddressToLocationsCompleted); locatorTask.Failed += new EventHandler<TaskFailedEventArgs>(locatorTask_Failed); AddressToLocationsParameters addressParams = new AddressToLocationsParameters() { OutSpatialReference = MapApplication.Current.Map.SpatialReference }; Address address = parameter as Address; Dictionary<string, string> addressDict = addressParams.Address; addressDict.Add("SingleLine", address.SingleLine); //addressDict.Add("Street", address.Street); //addressDict.Add("City", address.City); //addressDict.Add("State", address.State); //addressDict.Add("Zip", address.ZipCode); locatorTask.AddressToLocationsAsync(addressParams); } private void locatorTask_Failed(object sender, TaskFailedEventArgs e) { if (Failed != null) Failed(this, e); } private void locatorTask_AddressToLocationsCompleted(object sender, AddressToLocationsEventArgs e) { if (Completed != null) Completed(this, e); } public event EventHandler<AddressToLocationsEventArgs> Completed; public event EventHandler<TaskFailedEventArgs> Failed; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Input; using ESRI.ArcGIS.Client.Extensibility; using ESRI.ArcGIS.Client.Tasks; namespace MyAddressLocationTool { public class AddressLocationViewModel : INotifyPropertyChanged { private AddressLocationConfigData data = null; public AddressLocationViewModel(Map map, AddressLocationConfigData data) { this.Map = map; this.data = data; ValidateConfigData(data); InputAddress = new Address(); AddressToLocation = new AddressToLocationCommand(); AddressToLocation.LocatorURL = data.AddressLocatorURL; AddressToLocation.Completed += AddressToLocation_Completed; AddressToLocation.Failed += AddressToLocation_Failed; } public Map Map { get; private set; } public Address InputAddress { get; private set; } public AddressToLocationCommand AddressToLocation { get; private set; } private void ValidateConfigData(AddressLocationConfigData addressLocationConfigData) { Exception ex = null; if (addressLocationConfigData == null) { MainApplication.Handle("Search Configuration Error", "AddressLocation configuration is null"); throw ex; } else if (string.IsNullOrWhiteSpace(addressLocationConfigData.AddressLocatorURL)) { MainApplication.Handle("Search Configuration Error", "Address Locator URL is empty"); throw ex; } } private string error; public string Error { get { return error; } private set { if (error != value) { error = value; OnPropertyChanged("Error"); } } } private List<AddressCandidate> matchCandidates; public List<AddressCandidate> MatchCandidates { get { return matchCandidates; } set { if (matchCandidates == null || !matchCandidates.Equals(value)) { matchCandidates = value; OnPropertyChanged("MatchCandidates"); } } } private void AddressToLocation_Completed(object sender, AddressToLocationsEventArgs e) { MatchCandidates = e.Results; } private void AddressToLocation_Failed(object sender, TaskFailedEventArgs e) { Error = e.Error.Message; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class Address : INotifyPropertyChanged { private string _singleline; public string SingleLine { get { return _singleline; } set { if (_singleline != value) _singleline = value; OnPropertyChanged("SingleLine"); } } //private string _street; //public string Street //{ // get // { // return _street; // } // set // { // if (_street != value) // _street = value; // OnPropertyChanged("Street"); // } //} //private string _city; //public string City //{ // get // { // return _city; // } // set // { // if (_city != value) // _city = value; // OnPropertyChanged("City"); // } //} //private string _state; //public string State //{ // get // { // return _state; // } // set // { // if (_state != value) // _state = value; // OnPropertyChanged("State"); // } //} //private string _zipCode; //public string ZipCode //{ // get // { // return _zipCode; // } // set // { // if (_zipCode != value) // _zipCode = value; // OnPropertyChanged("ZipCode"); // } //} public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class AddressToLocationCommand : ICommand { private Address address; private string locatorURL = string.Empty; public string LocatorURL { get { return locatorURL; } set { locatorURL = value; } } public bool CanExecute(object parameter) { Address newAddress = parameter as Address; if (address == null || !address.Equals(newAddress)) { if (address != null) address.PropertyChanged -= Address_PropertyChanged; address = newAddress; if (address != null) address.PropertyChanged += Address_PropertyChanged; } if (address == null) return false; return !string.IsNullOrEmpty(address.SingleLine); //return !string.IsNullOrEmpty(address.Street) && !string.IsNullOrEmpty(address.City) // && !string.IsNullOrEmpty(address.State) && !string.IsNullOrEmpty(address.ZipCode); } private void Address_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty); } public event EventHandler CanExecuteChanged; public void Execute(Object parameter) { //Locator locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US/GeocodeServer"); Locator locatorTask = new Locator(locatorURL); locatorTask.AddressToLocationsCompleted += new EventHandler<AddressToLocationsEventArgs>(locatorTask_AddressToLocationsCompleted); locatorTask.Failed += new EventHandler<TaskFailedEventArgs>(locatorTask_Failed); AddressToLocationsParameters addressParams = new AddressToLocationsParameters() { OutSpatialReference = MapApplication.Current.Map.SpatialReference }; Address address = parameter as Address; Dictionary<string, string> addressDict = addressParams.Address; addressDict.Add("SingleLine", address.SingleLine); //addressDict.Add("Street", address.Street); //addressDict.Add("City", address.City); //addressDict.Add("State", address.State); //addressDict.Add("Zip", address.ZipCode); locatorTask.AddressToLocationsAsync(addressParams); } private void locatorTask_Failed(object sender, TaskFailedEventArgs e) { if (Failed != null) Failed(this, e); } private void locatorTask_AddressToLocationsCompleted(object sender, AddressToLocationsEventArgs e) { if (Completed != null) Completed(this, e); } public event EventHandler<AddressToLocationsEventArgs> Completed; public event EventHandler<TaskFailedEventArgs> Failed; } }
<StackPanel Orientation="Vertical"> <TextBlock Width="299" Margin="0,0,0,10" HorizontalAlignment="Center" FontWeight="Bold" Text="Enter Address Information" /> <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> <TextBlock Width="80" VerticalAlignment="Center" Text="Address: " TextAlignment="Right" /> <TextBox x:Name="SingleLine" Width="280" TabIndex="1" Text="{Binding Path=InputAddress.SingleLine, Mode=TwoWay}"> <i:Interaction.Behaviors> <local:UpdateBindingOnTextChanged /> </i:Interaction.Behaviors> </TextBox> </StackPanel> <Button x:Name="FindAddressButton" Width="80" Height="24" Margin="0,10,0,0" TabIndex="2" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Command="{Binding AddressToLocation}" CommandParameter="{Binding InputAddress}" Content="Find"> </Button> </StackPanel>
For geocoding services use the "Search" tool and configure it to use your geocoding service.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.