Select to view content in your preferred language

How the ???Geocoding Service??? can be integrated in the ???ArcGIS View for Silverlight????

2290
5
Jump to solution
09-27-2013 09:59 AM
JamalNUMAN
Legendary Contributor
How the ???Geocoding Service??? can be integrated in the ???ArcGIS View for Silverlight????

I???m wondering if there is a way to integrated the ???Geocoding Service??? with the environment of the ???ArcGIS View for Silverlight???

[ATTACH=CONFIG]27827[/ATTACH]

Thank you

Best

Jamal
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
1 Solution

Accepted Solutions
brettangel
Frequent Contributor
For geocoding services use the "Search" tool and configure it to use your geocoding service.

View solution in original post

0 Kudos
5 Replies
brettangel
Frequent Contributor
For geocoding services use the "Search" tool and configure it to use your geocoding service.
0 Kudos
JamalNUMAN
Legendary Contributor
For geocoding services use the "Search" tool and configure it to use your geocoding service.


Many thanks brett for the help,

I couldn�??t figure out how to include the geocoding service in the �??search�?� tool (screenshot below)

[ATTACH=CONFIG]28829[/ATTACH]
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
PietaSwanepoel2
Frequent Contributor
bangel:
you might be referring to the geometry service setting. For people outside the US, we have to create our own data and services

Jamal:
You will have to write your own address locator tool. Depending on how you created the address locator service you will either consume a single line (the supplied address is in format number street, suburb, town, country, etc...) of the elements individually

example code for single-line search: (not complete but you should get the idea)
Front-end:
        
<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>


code behind code to follow in next post
0 Kudos
PietaSwanepoel2
Frequent Contributor
Jamal:  rest of example code

Code 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;
    }
}
0 Kudos
JamalNUMAN
Legendary Contributor
Jamal:  rest of example code

Code 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;
    }
}



Thank you very much Pieta for the help. This is very useful.

I�??ll see if it works with me

Best

Jamal
----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos