Select to view content in your preferred language

Identify from interactive SDK

1315
11
09-28-2010 11:02 AM
DavidAshton
Frequent Contributor
I'm using the identify from the interactive SDK
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Identify
It works good but I would like to change when it fires.  Instead of using the left mouse click I was wondering if it is possible to use double click, right mouse click, or team it up with an activitatoin button. My endusers are missing firing identifies on a regular basis. Can someone help with this?


        private void QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = e.MapPoint;

            ESRI.ArcGIS.Client.Tasks.IdentifyParameters identifyParams = new IdentifyParameters()
            {
                Geometry = clickPoint,
                MapExtent = MyMap.Extent,
                Width = (int)MyMap.ActualWidth,
                Height = (int)MyMap.ActualHeight,
                LayerOption = LayerOption.visible
            };

0 Kudos
11 Replies
dotMorten_esri
Esri Notable Contributor
You can use the Map.MouseRightButtonUp event instead of the Click event.
To get the clickpoint in map coordinates where the click happened, simply do MyMap.ScreenToMap(e.GetPosition(MyMap)) in the eventhandler.
0 Kudos
DavidAshton
Frequent Contributor
Thanks for the reply Morten,

I'm sorry I'm still really green to programming and I can't figure out what to change. 

You can use the Map.MouseRightButtonUp event instead of the Click event.
To get the clickpoint in map coordinates where the click happened, simply do MyMap.ScreenToMap(e.GetPosition(MyMap)) in the eventhandler.



Here's my code:


   private void QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = e.MapPoint;

            ESRI.ArcGIS.Client.Tasks.IdentifyParameters identifyParams = new IdentifyParameters()
            {
                Geometry = clickPoint,
                MapExtent = Map.Extent,
                Width = (int)Map.ActualWidth,
                Height = (int)Map.ActualHeight,
                LayerOption = LayerOption.visible
            };

            IdentifyTask identifyTask = new IdentifyTask("http://MapServer");
            identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
            identifyTask.Failed += IdentifyTask_Failed;
            identifyTask.ExecuteAsync(identifyParams);

            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();
            ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
            {
                Geometry = clickPoint,
                Symbol = LayoutRoot.Resources["DefaultPictureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
            };
            graphicsLayer.Graphics.Add(graphic);
        }

        public void ShowFeatures(List<IdentifyResult> results)
        {
            _dataItems = new List<DataItem>();

            if (results != null && results.Count > 0)
            {
                IdentifyComboBox.Items.Clear();

                            
                
                
                foreach (IdentifyResult result in results)
                {
                    Graphic feature = result.Feature;
                    
                    string title = result.Value.ToString() + " (" + result.LayerName + ")";
                    _dataItems.Add(new DataItem()
                   
                    {
                        Title = title,
                        Data = feature.Attributes
                         
                    });
                    IdentifyComboBox.Items.Add(title);
                }
                IdentifyComboBox.SelectedIndex = 0;
            }
        }

        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = IdentifyComboBox.SelectedIndex;
            if (index > -1)
                IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
        }

        private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
        {
            IdentifyDetailsDataGrid.ItemsSource = null;

            if (args.IdentifyResults != null && args.IdentifyResults.Count > 0)
            {
                IdentifyResultsPanel.Visibility = Visibility.Visible;

                ShowFeatures(args.IdentifyResults);
            }
            else
            {
                IdentifyComboBox.Items.Clear();
                IdentifyComboBox.UpdateLayout();

                IdentifyResultsPanel.Visibility = Visibility.Collapsed;
                ShowIDtab.Begin();
            }
        }

        public class DataItem
        {
            public string Title { get; set; }
            public IDictionary<string, object> Data { get; set; }
        }

        void IdentifyTask_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show("Identify failed. Error: " + e.Error);
        }





AND THEN MY XAML

 
 <esri:Map x:Name="Map" Background="White" IsLogoVisible="False" Extent="-120.938900675217, 31.7429253923314, -114.082030001928, 38.2302170270371" Loaded="Map_Loaded"  MouseClick="QueryPoint_MouseClick" >



Thanks kindly for the time.
0 Kudos
JenniferNery
Esri Regular Contributor
You just need to some pieces in your code:
In XAML, change MouseClick="QueryPoint_MouseClick" to MouseRightButtonDown="QueryPoint_MouseClick"
In code-behind, change
private void QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = e.MapPoint;

to
  private void QueryPoint_MouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true; // to get rid of the default "Silverlight" Context Menu.
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = this.MyMap.ScreenToMap(e.GetPosition(MyMap));

rest of the code can stay the same.
0 Kudos
JMcNeil
Deactivated User
You just need to some pieces in your code:
In XAML, change MouseClick="QueryPoint_MouseClick" to MouseRightButtonDown="QueryPoint_MouseClick"
In code-behind, change
private void QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = e.MapPoint;

to
  private void QueryPoint_MouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true; // to get rid of the default "Silverlight" Context Menu.
            ESRI.ArcGIS.Client.Geometry.MapPoint clickPoint = this.MyMap.ScreenToMap(e.GetPosition(MyMap));

rest of the code can stay the same.





THANK YOU JENNIFER
0 Kudos
NancyHernandez
Emerging Contributor
Hello!! I have a similar doubt, but I would like to know how it could works with a button

thanks!!
0 Kudos
AliMirzabeigi
Emerging Contributor
Nancititla,

Could you please explain your use-case a little more? Also, are you using a button or a toggle button? The reason I'm asking these is that the identify tool ultimately requires users to click on the map control.
0 Kudos
NancyHernandez
Emerging Contributor
Thanks for the reply Ali  :),

I'm actually working with a button, the code in Xalm is:

<Button Style="{StaticResource darkButtonStyle}" Background="#FF1F5348" Width="42" x:Name="btnidentificar"  ToolTipService.ToolTip="Identificador" Click ="btnidentificar_Click"/>

and what I want is that when the button click event begin, the identify work restricted to the activation of the button.

I tried this in my file .cs but didn't works

private void btnidentificar_Click(object sender, EventArgs e)
        {
         QueryPoint_MouseClick();
  }

QueryPoint_MouseClick is the event that works directly on the map control

The code of identify is the same from
http://resources.esri.com/help/9.3/arcgisserver/apis/silverlight/samples/start.htm#Identify
    
so it's better if I use a toggle botton? how it works?

Thanks!!
0 Kudos
AliMirzabeigi
Emerging Contributor
Using Button or ToggleButton will depend on your use-case. To use a ToggleButton you can simply check for its "IsChecked" value in "QueryPoint_MouseClick" handler and return when the button is toggled, i.e.:
privatevoid QueryPoint_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
 if (!btnidentificar.IsChecked.Value)
    return;
 // Existing code here...
}

And the code in your XAML would be something similar to this:
<ToggleButton x:Name="btnidentificar" Content="Identify" Width="50" Height="50" Margin="5"
                    VerticalAlignment="Top" ToolTipService.ToolTip="Identificador" />

If you were to use a Button control instead you can have a global boolean flag in your user control and set it to TRUE/FALSE when your button is clicked and in "QueryPoint_MouseClick" handler check against that flag and return if it's not TRUE.
0 Kudos
NancyHernandez
Emerging Contributor
Well I change my original button to a toggle button, sorry but I'm learning this kind of languaje and it's a little complicated to me understand this 😮

If you were to use a Button control instead you can have a global boolean flag in your user control and set it to TRUE/FALSE when your button is clicked and in "QueryPoint_MouseClick" handler check against that flag and return if it's not TRUE.


I try what you suggested, but the toggle button inactivating the funcionality, when it's enabled does not run the Identify,

the event is normally initiated when you load the map without press any button and select a map item displays the result :confused:

What I should do? thanks!!
0 Kudos