AddresstoLocation for two Addresses

324
2
03-28-2012 10:27 AM
JoshPope
New Contributor II
I would like for a user to enter two addresses (origin & destination) send those through AddressLocator and display the addresscandidates on the map.
I have based my code on the sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AddressToLocation

I have expanded the XAML StackPanel to handle a duplicate set of TextBoxes for the second address inputs (InputAddress2, City2...etc) and in the code behind I have duplicated  the AddressToLocationParameters class building a second addressparams2 from a Dictionary based on the second set of input TextBoxes, but the locatorTask doesn't really appreciate that line of thought.

What is the appropriate way of passing two addresses through the AddresstoLocation task to display the results on the map?

Josh
0 Kudos
2 Replies
JoshPope
New Contributor II
For those encountering this issue in the future I realized the following solution.
I'm sure there's a more elegant way, but this seems to be doing the trick.

The main thing I realized was that I needed to create a new _locatorTask (i.e. _locatorTask2) AND to set it as a Locator resource ( i.e. Locator _locatorTask, _locatorTask2; ).
Another item to note that is different from the SL sample is to move the GraphicsLayer.ClearGraphics code to immediately under the FindAddressButton_Click event.  Both geocode tasks were working but only one graphic was showing because the first one was getting cleared when the ClearGraphics code snippet was in its original place.


 public partial class MainPage : UserControl
    {
        Locator _locatorTask, _locatorTask2;
        GraphicsLayer _candidateGraphicsLayer;
        private static ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
            new ESRI.ArcGIS.Client.Projection.WebMercator();

        public MainPage()
        {
            InitializeComponent();

            _candidateGraphicsLayer = MyMap.Layers["CandidateGraphicsLayer"] as GraphicsLayer;
        }

        private void FindAddressButton_Click(object sender, RoutedEventArgs e)
        {
            _candidateGraphicsLayer.ClearGraphics();
            CandidateListBox.Items.Clear();

            _locatorTask = new Locator("http://gismaps.pagnet.org/ArcGIS/rest/services/PAGAddressLocator2/GeocodeServer");
            _locatorTask.AddressToLocationsCompleted += LocatorTask_AddressToLocationsCompleted;
            _locatorTask.Failed += LocatorTask_Failed;

            AddressToLocationsParameters addressParams1 = new AddressToLocationsParameters()
            {
                OutSpatialReference = MyMap.SpatialReference
            };

            Dictionary<string, string> address1 = addressParams1.Address;

            if (!string.IsNullOrEmpty(InputAddress1.Text))
                address1.Add("Street", InputAddress1.Text);
            if (!string.IsNullOrEmpty(City1.Text))
                address1.Add("City", City1.Text);
            if (!string.IsNullOrEmpty(State1.Text))
                address1.Add("State", State1.Text);
            if (!string.IsNullOrEmpty(Zip1.Text))
                address1.Add("ZIP", Zip1.Text);


            _locatorTask.AddressToLocationsAsync(addressParams1);   

        {
            _locatorTask2 = new Locator("http://gismaps.pagnet.org/ArcGIS/rest/services/PAGAddressLocator2/GeocodeServer");
            _locatorTask2.AddressToLocationsCompleted += LocatorTask_AddressToLocationsCompleted;
            _locatorTask2.Failed += LocatorTask_Failed;


            AddressToLocationsParameters addressParams2 = new AddressToLocationsParameters()
            {
                OutSpatialReference = MyMap.SpatialReference
            };

            Dictionary<string, string> address2 = addressParams2.Address;

            if (!string.IsNullOrEmpty(InputAddress2.Text))
                address2.Add("Street", InputAddress2.Text);
            if (!string.IsNullOrEmpty(City2.Text))
                address2.Add("City", City2.Text);
            if (!string.IsNullOrEmpty(State2.Text))
                address2.Add("State", State2.Text);
            if (!string.IsNullOrEmpty(Zip2.Text))
                address2.Add("ZIP", Zip2.Text);

            _locatorTask2.AddressToLocationsAsync(addressParams2);
        }
       
        }
0 Kudos
EdithFergus
New Contributor
anyone else?
0 Kudos