Select to view content in your preferred language

How to obtain feature ID

4891
27
11-16-2010 10:18 AM
DonFreeman
Emerging Contributor
Is there a sample around that shows how to obtain the feature ID (place it into a variable) when the user clicks on a point feature?
Silverlight 4

Thanks
0 Kudos
27 Replies
DonFreeman
Emerging Contributor
You can do this by listening to the FeatureLayer_MouseLeftButtonUp event handler. Use the following code snippet to get the attribute collection of the Graphic object that has been clicked by the user:
IDictionary<string, object> attributes = e.Graphic.Attributes;


TaDa!! It works. Thanks so much to both of you.
  private void FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs args)
  {
   FeatureLayer featureLayer = sender as FeatureLayer;

   for (int i = 0; i < featureLayer.SelectionCount; i++)
    featureLayer.SelectedGraphics.ToList().UnSelect();

   args.Graphic.Select();
   IDictionary<string, object> attributes = args.Graphic.Attributes;

   MessageBox.Show("This FID = " + attributes["FID"]) ;

  }


Now, is there a way I can change the symbol for the selected and unselected points in the FeatureLayer?
0 Kudos
AliMirzabeigi
Emerging Contributor
Please take a look a the following SDK sample to get the whole idea:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
You would need to set the FeatureSymbol property of your FeatureLayer to your custom symbol.
0 Kudos
DonFreeman
Emerging Contributor
Please take a look a the following SDK sample to get the whole idea:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
You would need to set the FeatureSymbol property of your FeatureLayer to your custom symbol.


The sample uses graphics, I am using a FeatureLayer. Meanwhile I found this information.
http://help.arcgis.com/en/webapi/silverlight/help/creating_featurelayer.htm
Using the info therein I coded my map to match their sample like this.
<Grid.Resources>
    <esriSymbols:SimpleMarkerSymbol x:Name="MyMarkerSymbol" Color="Red" Style="Diamond" Size="10" />
   </Grid.Resources>
  
   <esri:Map x:Name="MyMap" Grid.RowSpan="2" >
    <esri:Map.Layers>
     <esri:ArcGISDynamicMapServiceLayer ID="SchoolSearch3" 
      Url="http://198.182.104.173/ArcGIS/rest/services/SchoolSearch3/MapServer"       VisibleLayers="0,1"/>
     <esri:FeatureLayer ID="MyFeatureLayer"
      Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" 
      MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"
      DisableClientCaching="True"
      Mode="OnDemand"
      OutFields="*" 
      SelectionColor="Yellow"     
      FeatureSymbol="{StaticResource MyMarkerSymbol}" >

     </esri:FeatureLayer>
    </esri:Map.Layers>
   </esri:Map>
which seems to work as expected except that I don't get the yellow selected color. However, when I substitute MY url in place of the sample, (Url="http://198.182.104.173/ArcGIS/rest/services/Intersection_Test/MapServer/0") it does not work. So the conclusion is that I must need to do something to the service to make this work? Correct? and if so what?

Thanks
0 Kudos
AliMirzabeigi
Emerging Contributor
If you can't see your layer on the map you may be using an ArcGIS Server service over a scheme different than the host application, please make sure you have the clientaccesspolicy.xml on your services Web server. More info here: http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2009/08/31/Using-services-across-schemes.aspx

If the above is not the problem and you can see your layer then if your layer has a Renderer defined in its service you can replace it with your own Renderer and use FeatureLayer's Renderer property to override the Renderer used in your FeatureLayer, i.e.
<esri:FeatureLayer ID="MyFeatureLayer" Renderer="{StaticResource PointSymbol}" ... />

Where:
<Grid.Resources>
 <esri:MarkerSymbol x:Key="SelectMarkerSymbol" >
  <esri:MarkerSymbol.ControlTemplate>
   <ControlTemplate>
    <Ellipse x:Name="Element" Width="15" Height="15" StrokeThickness="10" 
       Stroke="Green" >
     <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="SelectionStates">
       <VisualState x:Name="Unselected" />
       <VisualState x:Name="Selected">
        <Storyboard>
         <ColorAnimation Storyboard.TargetName="Element" 
          Storyboard.TargetProperty="(Ellipse.Stroke).(SolidColorBrush.Color)"
          To="Cyan" Duration="00:00:0.25"/>
        </Storyboard>
       </VisualState>
      </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>
    </Ellipse>
   </ControlTemplate>
  </esri:MarkerSymbol.ControlTemplate>
 </esri:MarkerSymbol>
 <esri:SimpleRenderer x:Key="PointSymbol" Symbol="{StaticResource SelectMarkerSymbol}" />
</Grid.Resources>
0 Kudos
DonFreeman
Emerging Contributor
Its not a problem with cross domain and yes I can see the layer in my map, I just can't get it to show with a different symbol. How would my service have been defined with a built in Renderer? I can modify the service if necessary to avoid additional coding to deal with it.
0 Kudos
JenniferNery
Esri Regular Contributor
You can look at this help doc http://help.arcgis.com/en/arcgisserver/10.0/help/arcgis_server_dotnet_help/index.html#/Authoring_fea...

There is a section on defining symbology.
0 Kudos
DonFreeman
Emerging Contributor
You can look at this help doc http://help.arcgis.com/en/arcgisserver/10.0/help/arcgis_server_dotnet_help/index.html#/Authoring_fea...

There is a section on defining symbology.


Do I infer correctly that to make use of FeatureSymbols the FeatureLayer must be based on a FeatureService and not a MapService?

Thanks
0 Kudos
JenniferNery
Esri Regular Contributor
FeatureSymbol is used when the FeatureLayer (regardless if it comes from Feature Service or Map Service) does not already have a Renderer.

You can define the Renderer in the service itself or in the application. The help doc in my previous post describes how it's done on the service.

http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLa...

Here's a sample how Renderer can be defined in your application: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerRendering
0 Kudos