Select to view content in your preferred language

Identify..?

1760
10
07-27-2010 01:18 PM
JoshV
by
Regular Contributor
Hello All-

In the samples on the Resource Center I was able to add the Identify sample to my Silverlight (3.0) App and get it to work.  I now want to add that as a Tool to my Toolbar.  Below is the main section of code in the XAML so does anyone know how I can add it as a ToolbarItem?

Many thanks,

<!-- Identity-->
        <Canvas  HorizontalAlignment="Stretch"  VerticalAlignment="Center" Grid.Row="0" Grid.Column="2">
            <StackPanel Orientation="Vertical" Margin="10" >
                <Grid x:Name="IdentifyGrid" HorizontalAlignment="Stretch"  VerticalAlignment="Center"  Margin="0,7,7,0" >
                    <Rectangle Fill="#22000000" RadiusX="10" RadiusY="10" Margin="0,4,0,0" />
                    <Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Margin="0,0,0,5" />
                    <TextBlock x:Name="DataDisplayTitleTop" Text="Click on map to identify a feature" Foreground="Black" FontSize="10" 
                      Margin="10,5,0,0" />
                    <TextBlock x:Name="DataDisplayTitleBottom" Text="Click on map to identify a feature" Foreground="White" FontSize="10" 
                      Margin="10,3,0,5" />
                    <StackPanel Orientation="Vertical" Margin="15" HorizontalAlignment="Right" VerticalAlignment="Center">
                        <ComboBox x:Name="IdentifyComboBox" MinWidth="150" SelectionChanged="cb_SelectionChanged"
                         Margin="5,10,5,5" >
                        </ComboBox>
                        <ScrollViewer x:Name="DataGridScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"  
                         Width="230" MaxHeight="340" Visibility="Collapsed">
                            <slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="None" 
                              Background="White" >
                                <slData:DataGrid.Columns>
                                    <slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
                                    <slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
                                </slData:DataGrid.Columns>
                            </slData:DataGrid>
                        </ScrollViewer>
                    </StackPanel>
                </Grid>
            </StackPanel>
        </Canvas>
0 Kudos
10 Replies
PreetiMaske
Esri Regular Contributor
Actually it is pretty simple. All you need to do is add a toolbaritem to toolbar for identify tool.
<esri:ToolbarItem Text="Identify">
                <esri:ToolbarItem.Content>
                  <Image Source="/Assets/images/i_about.png" Stretch="UniformToFill" Margin="5" />
                </esri:ToolbarItem.Content>
              </esri:ToolbarItem>

Create a bool variable and set it true only when "Identify" is clicked.
bool identifyclick;
private void MyToolbar_ToolbarItemClicked(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
    {
case 7: //Identify
          identifyclick = true;
          break;

      }
    }

In Map Mouseclick event check value for this boolean variable to true before running identify logic
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
    {
      if (identifyclick)
      {
        //execute identify code

        identifyclick = false // at the end reset the bool variable to deactivate the identify tool.
       }
     }

Basically it will be combination of ToolbarWidget sample and Identify Sample. Most of the code can be copied from Identify sample and pasted as is to toolbarWidget Sample.

Attached is the sample XAML with implementation. Hope that helps
0 Kudos
JoshV
by
Regular Contributor
Actually it is pretty simple. All you need to do is add a toolbaritem to toolbar for identify tool.
<esri:ToolbarItem Text="Identify">
                <esri:ToolbarItem.Content>
                  <Image Source="/Assets/images/i_about.png" Stretch="UniformToFill" Margin="5" />
                </esri:ToolbarItem.Content>
              </esri:ToolbarItem>

Create a bool variable and set it true only when "Identify" is clicked.
bool identifyclick;
private void MyToolbar_ToolbarItemClicked(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
    {
case 7: //Identify
          identifyclick = true;
          break;

      }
    }

In Map Mouseclick event check value for this boolean variable to true before running identify logic
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
    {
      if (identifyclick)
      {
        //execute identify code

        identifyclick = false // at the end reset the bool variable to deactivate the identify tool.
       }
     }

Basically it will be combination of ToolbarWidget sample and Identify Sample. Most of the code can be copied from Identify sample and pasted as is to toolbarWidget Sample.

Attached is the sample XAML with implementation. Hope that helps


Hi Preeti-

When I tried to save and open that zip file it said it was corrupt and could not open.  Could you just paste the Identify code needed for taht last part of your post?

Thanks Preeti
0 Kudos
PreetiMaske
Esri Regular Contributor
You can copy the XAML below and make the following additons to the Toolbar sample's codebehind

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 ToolBarWidget : UserControl
  {
    private List<DataItem> _dataItems = null;
    bool identifyclick = false;

  
    private void MyToolbar_ToolbarItemClicked(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
    {
      MyDrawObject.IsEnabled = false;
      _toolMode = "";
      switch (e.Index)
      {
       
        case 7: //Identify
          identifyclick = true;
          break;

      }
    }

  
      private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
    {
      if (identifyclick)
      {
        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
        };

        IdentifyTask identifyTask = new IdentifyTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
            "Demographics/ESRI_Census_USA/MapServer");
        identifyTask.ExecuteCompleted += IdentifyTask_ExecuteCompleted;
        identifyTask.Failed += IdentifyTask_Failed;
        identifyTask.ExecuteAsync(identifyParams);

        GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] 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);
        identifyclick = false;
      }
      if (IdentifyBorder.Visibility == Visibility.Visible)
        IdentifyBorder.Visibility = Visibility.Collapsed;
    }

    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)
      {
        IdentifyBorder.Visibility = Visibility.Visible;
        IdentifyResultsPanel.Visibility = Visibility.Visible;

        ShowFeatures(args.IdentifyResults);

      }
      else
      {
        IdentifyComboBox.Items.Clear();
        IdentifyComboBox.UpdateLayout();

        IdentifyResultsPanel.Visibility = Visibility.Collapsed;
      }
    }

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

    private void DataDisplayTitleBottom_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
      IdentifyBorder.Visibility = Visibility.Collapsed;
    }
  }
}


===================== XAML ============================================
<UserControl x:Class="ArcGISSilverlightSDK.ToolBarWidget"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" >
 
  <Grid x:Name="LayoutRoot" Background="White">

    <Grid.Resources>
      <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red"
                                          BorderThickness="2" />
      <esri:PictureMarkerSymbol x:Key="DefaultPictureSymbol" OffsetX="35" OffsetY="35"
                 Source="/Assets/images/i_about.png" />
    </Grid.Resources>

    <esri:Map x:Name="MyMap" Extent="-33,-12.85,99.75,87.15"
                  ExtentChanged="MyMap_ExtentChanged" MouseClick="MyMap_MouseClick">
      <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
                    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
      <esri:GraphicsLayer ID="MyGraphicsLayer" />
    </esri:Map>

    <Grid Height="110" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0" >
      <Rectangle Fill="#77919191" RadiusX="10" RadiusY="10" Margin="0,4,0,6" >
        <Rectangle.Effect>
          <DropShadowEffect/>
        </Rectangle.Effect>
      </Rectangle>
      <Rectangle Fill="#CCFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15" />
      <StackPanel Orientation="Vertical">
        <esri:Toolbar x:Name="MyToolbar" MaxItemHeight="80" MaxItemWidth="80"
                    VerticalAlignment="Top" HorizontalAlignment="Center"
                    Loaded="MyToolbar_Loaded"
                    ToolbarItemClicked="MyToolbar_ToolbarItemClicked"
                    ToolbarIndexChanged="MyToolbar_ToolbarIndexChanged"
                    Width="450" Height="80">
          <esri:Toolbar.Items>
            <esri:ToolbarItemCollection>
                            <esri:ToolbarItem Text="Identify">
                <esri:ToolbarItem.Content>
                  <Image Source="/Assets/images/i_about.png" Stretch="UniformToFill" Margin="5" />
                </esri:ToolbarItem.Content>
              </esri:ToolbarItem>
            </esri:ToolbarItemCollection>
          </esri:Toolbar.Items>
        </esri:Toolbar>
        <TextBlock x:Name="StatusTextBlock" Text="" FontWeight="Bold" HorizontalAlignment="Center"/>
      </StackPanel>
    </Grid>

    <Border x:Name="IdentifyBorder" Background="#77919191" BorderThickness="1" CornerRadius="5"
                HorizontalAlignment="Right" BorderBrush="Gray" VerticalAlignment="Top"
                Margin="5">
      <Border.Effect>
        <DropShadowEffect/>
      </Border.Effect>
      <Grid x:Name="IdentifyGrid" HorizontalAlignment="Right" VerticalAlignment="Top" >
        <Grid.RowDefinitions>
          <RowDefinition Height="30" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock x:Name="DataDisplayTitleBottom" Text="Close"
                       Foreground="White" FontSize="10" Margin="206,4,6,2" HorizontalAlignment="Center" MouseLeftButtonDown="DataDisplayTitleBottom_MouseLeftButtonDown" >
                    <TextBlock.Effect>
                        <DropShadowEffect />                         
                    </TextBlock.Effect>
        </TextBlock>
        <Grid x:Name="IdentifyResultsPanel" Margin="5,1,5,5" HorizontalAlignment="Center"
                            VerticalAlignment="Top" Visibility="Collapsed" Grid.Row="1">
          <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <ComboBox x:Name="IdentifyComboBox" MinWidth="150" SelectionChanged="cb_SelectionChanged"
                         Margin="5,1,5,5" Grid.Row="0" >
          </ComboBox>
          <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" 
                         Width="230" MinHeight="200" Grid.Row="1">
            <slData:DataGrid x:Name="IdentifyDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="None"
                              Background="White">
              <slData:DataGrid.Columns>
                <slData:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
                <slData:DataGridTextColumn Binding="{Binding Path=Value}"/>
              </slData:DataGrid.Columns>
            </slData:DataGrid>
          </ScrollViewer>
        </Grid>
      </Grid>
    </Border>


  </Grid>
</UserControl>
0 Kudos
JoshV
by
Regular Contributor
Hi Preeti-

I took your suggestions and copied your C# and your XAML and it is nearly exactly the same now in my App.  The only difference is that I use a DynamicMapServiceLayer so in the MyMap_MouseClick event I changed the URL to piont to my local map service.  Everything else looks nearly the same to me but when I run my application and select the Identify button, it acts like it's active but when I select any layers in my dynamic service (wells) it never does anything.

Thoughts?
0 Kudos
PreetiMaske
Esri Regular Contributor
I think the spatialReference for the Wells layers is different than the basemap layer. The Extent that is going as parameter to identifytask is using the basemap's extent which probably does not match with coordinate system of the Wells layer and that is causing the identifyTask to not return any results. You can put a breakpoint in MyMap_MouseClick to see what is the extent passed to Identifytask parameters and also in IdentifyTask_ExecuteCompleted to see if any results are returned.

--Preeti
0 Kudos
JoshV
by
Regular Contributor
I think the spatialReference for the Wells layers is different than the basemap layer. The Extent that is going as parameter to identifytask is using the basemap's extent which probably does not match with coordinate system of the Wells layer and that is causing the identifyTask to not return any results. You can put a breakpoint in MyMap_MouseClick to see what is the extent passed to Identifytask parameters and also in IdentifyTask_ExecuteCompleted to see if any results are returned.

--Preeti


I thought the same thing but when I changed the MouseClick's Identify task to http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer  and then I declared my DynamicMapServiceLayer to be that as well, when I debugged the application it still did not work.  Shouldn't that URL be the same as the maps?  Could I change the WKID of the map to be what my DynamicService is?
0 Kudos
PreetiMaske
Esri Regular Contributor
It works fine for me...Can you send the code?
0 Kudos
JoshV
by
Regular Contributor
It works fine for me...Can you send the code?


Here is my code using the ESRI Rest URL's instead of my dynamicmapservice.  Thanks for taking a look at it..

(I included all of it so its attached)  The XAML is split into 2 files and the CS is in 2 files.  the size limitations..geez
0 Kudos
PreetiMaske
Esri Regular Contributor
You haven't added the handler for MouseClick for map in your XAML.

<esri:Map x:Name="MyMap" MouseMove="MyMap_MouseMove" Extent="-120, 30, -60, 60" MouseClick="MyMap_MouseClick" >

<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="AGOLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer ID="DynamicLayerCalifornia"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"
InitializationFailed="Layer_InitializationFailed" />
<esri:GraphicsLayer ID="MyGraphicsLayer" />

</esri:Map.Layers>
</esri:Map>

</esri:Map.Layers>
</esri:Map>
0 Kudos