ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventAr gs args Issue

941
6
08-04-2010 05:09 AM
jonataspovoas
New Contributor III
Hi,

My application has a Map and a Box to locate Adresses.

My locator is based on the example at http://resources.esri.com/help/9.3/arcgisserver/apis/silverlight/samples/start.htm#AddressToLocation, but the MSVisual Studio says that there's this error on my code.
 MainPage.Localizador_LocationToAddressCompleted(object sender, AddressToLocationsEventArgs e) 

Error:
    No overload for 'Localizador_LocationToAddressCompleted' matches deleagate 'System.EventHandler<ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs>'



the problem is that the code on the link says to do so, and i have no idea how to make this work...

here's what i've done so far:

CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;

namespace AdressLocator
{
    public partial class MainPage : UserControl
    {
        List<AddressCandidate> _candidateList;
        private bool _firstZoom = true;
        private int _lastIndex = 0;

        Locator localizador = new Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/" +
                "ESRI_Geocode_USA/GeocodeServer");

        public MainPage()
        {
            InitializeComponent();
            btnAcheEncereço.Click += BtnAcheEncereço_click;
            //Here's where the Visual Studio point's the error:
            localizador.LocationToAddressCompleted += Localizador_LocationToAddressCompleted;
            localizador.Failed += new EventHandler<TaskFailedEventArgs>(Localizador_Failed);
        }

        private void BtnAcheEncereço_click(object sender, RoutedEventArgs e)
        {
            AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
            Dictionary<string, string> address = addressParams.Address;

            if (!string.IsNullOrEmpty(State.Text))
                address.Add("Address", Address.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);

            if (addressParams.Address.Count == 0)
            {
                GraphicsLayer graphicsLayer = mapa.Layers["MyGraphicsLayer"] as GraphicsLayer;
                if (graphicsLayer != null)
                    graphicsLayer.ClearGraphics();
                candidateScrollViewer.Content = null;
                candidatePanelGrid.Visibility = Visibility.Collapsed;
                MessageBox.Show("No address field entered.");
                return;
            }
                
            localizador.AddressToLocationsAsync(addressParams);
        }

        private void Localizador_LocationToAddressCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs e)
        {
            List<AddressCandidate> returnedCandidates = e.Results;

            if (returnedCandidates.Count == 0)
            {
                MessageBox.Show("No address match found.");
                return;
            }

            GraphicsLayer graphicsLayer = mapa.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();

            _candidateList = new List<AddressCandidate>();
            ListBox candidateListBox = new ListBox()
            {
                BorderThickness = new Thickness(0),
                VerticalAlignment = VerticalAlignment.Center
            };

            foreach (AddressCandidate candidate in returnedCandidates)
            {
                if (candidate.Score >= 65)
                {
                    _candidateList.Add(candidate);
                    candidateListBox.Items.Add(candidate.Address);

                    Graphic graphic = new Graphic()
                    {
                        Symbol = DefaultMarkerSymbol,
                        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);

                    graphicsLayer.Graphics.Add(graphic);
                }
            }

            candidateListBox.SelectionChanged += _candidateListBox_SelectionChanged;

            candidateScrollViewer.Content = candidateListBox;
            candidatePanelGrid.Visibility = Visibility.Visible;

            MapPoint pt = _candidateList[0].Location;
            if (_firstZoom)
            {
                mapa.ZoomToResolution(mapa.Resolution / 4, pt);
                _firstZoom = false;
            }
            else
                mapa.PanTo(pt);

            _lastIndex = 0;
            candidateListBox.SelectedIndex = 0;
        }

        void _candidateListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            int index = listBox.SelectedIndex;
            if (index >= 0 && _lastIndex != index)
            {
                _lastIndex = index;
                AddressCandidate candidate = _candidateList[index];
                mapa.PanTo(candidate.Location);
            }
        }


        private void Localizador_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.ToString());
        }
    }
}



Does Anyone knows how to fix it??
0 Kudos
6 Replies
DominiqueBroux
Esri Frequent Contributor

private void Localizador_LocationToAddressCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs e)
{



LocationToAddress delegate is waiting for an AddressEventArgs (not a AddressToLocationsEventArgs)
0 Kudos
jonataspovoas
New Contributor III
LocationToAddress delegate is waiting for an AddressEventArgs (not a AddressToLocationsEventArgs)


I already said that on my the post.

What I said that was the problem is that the ESRI SAMPLES of the ArcGIS API shows the code receiving ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs as parameter instead of AddressEventArgs, and my question was how do i make it work...

Did you check the code at the link to the ESRI samples?


using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

namespace ArcGISSilverlightSDK
{
    public partial class AddressToLocation : UserControl
    {
        List<AddressCandidate> _candidateList;
        private bool _firstZoom = true;
        private int _lastIndex = 0;

        public AddressToLocation()
        {
            InitializeComponent();
        }

        private void FindAddressButton_Click(object sender, RoutedEventArgs e)
        {
            Locator locatorTask = new Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
                "Locators/ESRI_Geocode_USA/GeocodeServer");
            locatorTask.AddressToLocationsCompleted += LocatorTask_AddressToLocationsCompleted;
            locatorTask.Failed += LocatorTask_Failed;
            AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
            Dictionary<string, string> address = addressParams.Address;           

            if (!string.IsNullOrEmpty(State.Text))
                address.Add("Address", Address.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);

            if (addressParams.Address.Count == 0)
            {
              GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
              if (graphicsLayer !=null)
                graphicsLayer.ClearGraphics();
              CandidateScrollViewer.Content = null;
              CandidatePanelGrid.Visibility = Visibility.Collapsed;
              MessageBox.Show("No address field entered.");
              return;
            }
              

            locatorTask.AddressToLocationsAsync(addressParams);
        }

        private void LocatorTask_AddressToLocationsCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs args)
        {
            List<AddressCandidate> returnedCandidates = args.Results;

            if (returnedCandidates.Count == 0)
            {
                MessageBox.Show("No address match found.");
                return;
            }

            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();

            _candidateList = new List<AddressCandidate>();
            ListBox candidateListBox = new ListBox()
            {
                BorderThickness = new Thickness(0),
                VerticalAlignment = VerticalAlignment.Center
            };


            foreach (AddressCandidate candidate in returnedCandidates)
            {
                if (candidate.Score >= 65)
                {
                    _candidateList.Add(candidate);
                    candidateListBox.Items.Add(candidate.Address);

                    Graphic graphic = new Graphic()
                    {
                        Symbol = DefaultMarkerSymbol,
                        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);

                    graphicsLayer.Graphics.Add(graphic);
                }
            }

            candidateListBox.SelectionChanged += _candidateListBox_SelectionChanged;

            CandidateScrollViewer.Content = candidateListBox;
            CandidatePanelGrid.Visibility = Visibility.Visible;

            MapPoint pt = _candidateList[0].Location;
            if (_firstZoom)
            {
                MyMap.ZoomToResolution(MyMap.Resolution / 4, pt);
                _firstZoom = false;
            }
            else
                MyMap.PanTo(pt);

            _lastIndex = 0;
            candidateListBox.SelectedIndex = 0;
        }

        void _candidateListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            int index = listBox.SelectedIndex;
            if (index >= 0 && _lastIndex != index)
            {
                _lastIndex = index;
                AddressCandidate candidate = _candidateList[index];
                MyMap.PanTo(candidate.Location);
            }
        }

        private void LocatorTask_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show("Locator service failed: " + e.Error);
        }
    }
}




This code is at the Samples Gallery at http://resources.esri.com/help/9.3/arcgisserver/apis/silverlight/samples/start.htm#AddressToLocation
0 Kudos
JenniferNery
Esri Regular Contributor
I don't see anything wrong with your code-behind. It looks similar to the sample. Maybe let VS create the Eventhandler for you by doing locatorTask += and then tab, see if this is any different to the LocatorTask_AddressToLocationsCompleted() you have. If this does not work, can you also share your XAML code?

Jennifer
0 Kudos
DominiqueBroux
Esri Frequent Contributor


LocationToAddress delegate is waiting for an AddressEventArgs (not a AddressToLocationsEventArgs)

I already said that on my the post.

What I said that was the problem is that the ESRI SAMPLES of the ArcGIS API shows the code receiving ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs as parameter instead of AddressEventArgs, and my question was how do i make it work...

Did you check the code at the link to the ESRI samples?

This code is at the Samples Gallery at http://resources.esri.com/help/9.3/arcgisserver/apis/silverlight/samples/start.htm#AddressToLocation


Sorry, I am not sure to understand.
I checked the ESRI samples, I didn't find any LocationToAddress sample which would use an AddressToLocationEventsArg (the link you are giving is for AddressToLocation, so it's OK).

Did I miss something?
0 Kudos
jonataspovoas
New Contributor III
Sorry, I am not sure to understand.
I checked the ESRI samples, I didn't find any LocationToAddress sample which would use an AddressToLocationEventsArg (the link you are giving is for AddressToLocation, so it's OK).


I am very sorry for all the trouble... just now i realized i am really doing all right, but i'm using the wrong event...

Someone murder me please...

i should have used localizador.AddressToLocationsCompleted but instead i used localizador.LocationToAddressCompleted

can you also share your XAML code?


Sure!


<UserControl x:Class="AdressLocator.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600" 
 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:userControls="clr-namespace:ESRI.ArcGIS.SilverlightMapApp"
    xmlns:actions="clr-namespace:ESRI.ArcGIS.SilverlightMapApp.Actions"
             
    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
    xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
    xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
    
    <UserControl.Resources>
        <esriSymbols:FillSymbol x:Name="DefaultMarkerSymbol" Fill="Red" BorderBrush="White" BorderThickness="2" />
        <esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" Background="{StaticResource BaseColor}">
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
  <!--Menu bar-->
        <Grid Grid.Row="0" x:Name="topMenuGrid" Background="{StaticResource BaseColor}">
            <Rectangle x:Name="BackgroundGradient" Opacity=".5" Fill="{StaticResource ReflectionGradient}" />
            <StackPanel Margin="0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <esri:MapProgressBar Width="200" Map="{Binding ElementName=Map}" Background="White" Foreground="Black"/>
            </StackPanel>
        </Grid>

        <!-- Body of the Application -->
        <Grid Grid.Row="1">
            <!-- Map View -->
            <esri:Map x:Name="mapa" Background="{StaticResource BaseColor}" >
                <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" 
            Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
                
                <esri:GraphicsLayer ID="Graficos">
                    <esri:GraphicsLayer.MapTip>
                        <Canvas>
                            <Grid>
                                <Rectangle Stroke="Gray" RadiusX="10" RadiusY="10" Fill="#77FF0000" Margin="0,0,0,5" />
                                <Rectangle Fill="#DDFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15" />
                                <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="30,20,30,30">
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
                                        ConverterParameter=Address, Mode=OneWay}" HorizontalAlignment="Left" />
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
                                        ConverterParameter=LatLon, Mode=OneWay}" HorizontalAlignment="Left" />
                                </StackPanel>
                            </Grid>
                        </Canvas>
                    </esri:GraphicsLayer.MapTip>

                </esri:GraphicsLayer>
            </esri:Map>

            <!-- Scale Bar -->
            <userControls:ScaleBar x:Name="scaleBar" Opacity=".5" Map="{Binding ElementName=Map}" 
                       MapUnit="Kilometers" Width="200" Fill="White" Margin="10,0,0,10" 
                                   IsHitTestVisible="False" HorizontalAlignment="Left" VerticalAlignment="Bottom" />

            <!-- Navigator -->
            <esri:Navigation x:Name="nav" Margin="10,0,0,20" Map="{Binding ElementName=Map}" 
                                HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                                Background="{StaticResource BaseColor}" PanFactor="0.1">
                <esri:Navigation.Effect>
                    <DropShadowEffect Color="Black" Direction="-45" BlurRadius="20" Opacity=".75" />
                </esri:Navigation.Effect>
            </esri:Navigation>
        </Grid>

        <Grid Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,15,0">
            <Rectangle Stroke="Gray"  RadiusX="10" RadiusY="10" Fill="#775C90B2" Margin="0,0,0,5" >
                <Rectangle.Effect>
                    <DropShadowEffect/>
                </Rectangle.Effect>
            </Rectangle>
            <Rectangle Fill="#DDFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="30,20,30,30">
                <TextBlock Text="Enter Address Information" FontWeight="Bold" HorizontalAlignment="Center" />
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" >
                    <TextBlock Text="Address: " Width="80" TextAlignment="Right" />
                    <TextBox x:Name="Address" Text="400 Market Street" Width="125"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" >
                    <TextBlock Text="City: " Width="80" TextAlignment="Right" />
                    <TextBox x:Name="City" Text="San Francisco" Width="125"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" >
                    <TextBlock Text="State: " Width="80" TextAlignment="Right"/>
                    <TextBox x:Name="State" Text="CA" Width="125"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" >
                    <TextBlock Text="Zip: " Width="80" TextAlignment="Right"/>
                    <TextBox x:Name="Zip" Text="94111" Width="125"/>
                </StackPanel>
                <Button x:Name="btnAcheEncereço" Content="Find" Width="100" HorizontalAlignment="Center" Margin="0,5,0,0" />
            </StackPanel>
        </Grid>

        <Grid Grid.Row="1" x:Name="candidatePanelGrid" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,15,15,0" Visibility="Collapsed">
            <Rectangle Stroke="Gray"  RadiusX="10" RadiusY="10" Fill="#775C90B2" Margin="0,0,0,5" >
                <Rectangle.Effect>
                    <DropShadowEffect/>
                </Rectangle.Effect>
            </Rectangle>
            <Rectangle Fill="#FFFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,17,10,15" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="10,2,15,15">
                <TextBlock HorizontalAlignment="Left" Text="Address Candidates" Margin="2,0,0,5" />
                <ScrollViewer x:Name="candidateScrollViewer" Width="300" MaxHeight="150" BorderThickness="0"
                              HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
                </ScrollViewer>
            </StackPanel>
        </Grid>


    </Grid>
</UserControl>

0 Kudos
DominiqueBroux
Esri Frequent Contributor
No problem, you are welcome, even alive 🙂
0 Kudos