Tag: featurelayer

FeatureLayer rendering: taking advantage of WebGL in 2D

webgl-featured-image

The 4.5 version of the ArcGIS API for JavaScript allows users to opt in to rendering FeatureLayer with WebGL (beta) in 2D MapViews. This is a major step in improving the overall performance of FeatureLayer, providing you with the ability … Continue reading

Posted in App Developers, Developer, Uncategorized, Web | Tagged , , , , , , , | 3 Comments

Listen for updates to a FeatureLayer

The ArcGIS Silverlight/WPF API provides a FeatureLayer as an efficient means for referencing a layer that contains features (geometry and attributes) and resides on a server.  You can add a FeatureLayer to a Map in XAML, define the URL to the layer on the server, add symbology and the feature layer contents will be rendered in the Map as graphic features.  Technically the FeatureLayer is a GraphicsLayer that wraps a QueryTask which queries either a feature layer in an ArcGIS Server map service or a spatial table via the MapIt Spatial Data Service. 

Unfortunately the FeatureLayer does not explicitly expose any events dealing with the query operation, such as when the query has completed and all features have been returned.   This may be necesssary if you need to define renderers on the fly, determine query progress, etc.  To determine when a FeatureLayer query is complete you can listen to the collection changed event on the FeatureLayer.Graphics property.  However this event will be triggered each time a graphic is added, so you need to wait until all graphics have been added.  Enter the Dispatcher class.  Dispatcher enables you to invoke a delegate on the UI (main) thread.  In this case, the completed event on the QueryTask inside the FeatureLayer is executing on the UI thread.  You can call the Dispatcher.BeginInvoke method and delegate a call to code of your choice.  Since this delegate must execute on the UI thread, it must also wait for the completed event on QueryTask to finish before being called.  So in effect, the delegate in BeginInvoke is waiting in a queue.  Also, so the delegate isn’t called each time the graphics collection changes, remove the collection changed event handler upon the first call.  You can readd the handler in your code logic if you need to continue listening to changes in a FeatureLayer’s graphics collection. A basic code example is provided below.  It assumes a FeatureLayer named “MyFeatureLayer” has been defined in XAML:

ESRI.ArcGIS.Client.FeatureLayer _featureLayer;

public FeatureLayerEventingDemo()
{
    InitializeComponent();

    _featureLayer = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
    _featureLayer.Graphics.CollectionChanged += Graphics_CollectionChanged;
}

void Graphics_CollectionChanged(object sender,
    System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    // Remove event method on the first call, then invoke delegate
    _featureLayer.Graphics.CollectionChanged -= Graphics_CollectionChanged;
    System.Windows.Application.Current.RootVisual.Dispatcher.BeginInvoke(
	delegate {
	    doSomething();
	});
}

private void doSomething()
{
    // Do something... then readd event method to listen for another update
    _featureLayer.Graphics.CollectionChanged += Graphics_CollectionChanged;
}
Update: As of version 1.2, FeatureLayer maintains an UpdateCompleted to listen for when features have been retrieved from a service and an UpdateFailed event to determine if an error occurred during feature retrieval.

Rex Hansen
ESRI Product Engineer
ArcGIS Server .NET, Silverlight/WPF, MapIt

 

Posted in Web | Tagged , , | 2 Comments