The ArcGIS JavaScript API version 2.0 offers a new feature layer for working with client-side graphic features. You may be familiar with the graphics layer from previous releases. The feature layer inherits from the graphics layer, but offers additional capabilities such as the ability to perform queries and selections. Feature layers are also used for Web editing.
Feature layers differ from tiled and dynamic map service layers because feature layers bring geometry information across to the client computer to be drawn by the Web browser. Feature layers potentially cut down on round trips to the server. A client can request the features it needs, then perform selections and queries on those features without having to request more information from the server. Feature layers are especially appropriate for layers that respond to user interaction, such as a mouse click or hover.
A feature layer honors any definition queries, scale dependencies, and other properties configured on the layer in the map service. Using a feature layer, you can access related tables, perform queries, display time slices, work with feature attachments, and do various other useful things.
Feature layers can be created by referencing a layer from either a map service or a feature service or by specifying a feature collection object.
When you create a feature service, you also need to specify a mode for retrieving features. Because the mode determines when and how features are brought from the server to the client, your choice can affect the speed and appearance of your application. You have the following mode choices:
Note: No matter which of the display modes you choose for your feature layer, selected features are always held on the client. For example, if you are using the on demand display mode and you've selected some features, panning away from those selected features will still result in the features being available on the client. Your application will not have to re-select the features or make any other request to the server if you pan back to the original area. For this reason, it's a good practice to clear your selected features once they are no longer needed.
Feature layers not only retrieve feature geometries; they can also get attribute information. When you create the feature layer, you can specify which attributes, or "out fields" will be retrieved for your features. It's possible to just request "*" to get all the fields, but to reduce the amount of information sent between the client and server, you should request only the fields you need in your application. If you later perform a query using the feature layer, the query will honor the out fields you set.
Feature layers make it possible for you to do all of the following:
Feature layers are the key to editing features with the Web APIs. The editing widgets included in the APIs are designed to work with feature layers. You can only perform editing on feature layers that reference a feature service. See Editing to learn more about this requirement and how to create a feature service.
There may be times where you want to work with features that satisfy certain attribute criteria and disregard the rest. In this type of situation, you can set a definition expression on the feature layer to retrieve just the features you need. Every feature layer has a default definition expression, which comes from the definition query set in the source map document. You can set a new definition expression as you work with the feature layer.
You can use a feature layer to isolate features that meet a certain temporal criteria. For example, you might want to show only houses that were constructed before 1930. If your house layer is time-aware, you can set a time definition on the feature layer to show only houses whose construction dates were before 1930.
The feature layer supports a selection, which represents a subset of features that the user has chosen to isolate for viewing, editing, or analytical purposes. Users can add or remove features from the selection based on spatial or attribute criteria. The feature layer makes it easy to draw the selection set with a different type of symbol than the other features. This was previously challenging to do without using two graphics layers.
A selection set can be used as input for statistical calculations, geoprocessing tools, and graphing, opening the possibility for interactive GIS analysis and exploration. For example, selections could allow a user to draw a polygon around a set of buildings, then push a button to calculate the solar potential of those buildings. Selections also play an important role in some editing workflows where the feature currently undergoing edits is displayed as selected.
To make your selection, use the FeatureLayer.SelectFeatures() method, which takes a Query object as an argument. If you're just interested in querying your feature layer without adding the results to the selection set, you can use either QueryFeatures() or QueryRelatedRecords() instead. With any of these methods, you cannot request features outside of any definition expressions set in the map document or on the feature layer.
The ArcGIS JavaScript API has several samples that show how to work with feature layers. Here are a few you may find useful:
Select features using a feature layer