ArcGIS Runtime SDK for Java

Feature Layer Selection

Demonstrates how to select Features from a FeatureLayer.

How it works

To select Features from your FeatureLayer:

  1. Create a ServiceFeatureTable from a URL.
  2. Create a feature layer from the service feature table.
  3. Identify MapView on the location the user has clicked.
    • call MapView.identifyLayerAsync(FeatureLayer, Point2D, tolerance, max results)
    • feature layer, from above
    • Point2D, location where the user clicked
    • tolerance, area around Point2D to locate features
    • max results, maximum number of features to return
  4. Select all features that were identified, FeatureLayer.selectFeatures().


 * Copyright 2017 Esri.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.

package com.esri.samples.featurelayers.feature_layer_selection;

import java.util.List;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;
import com.esri.arcgisruntime.mapping.view.MapView;

public class FeatureLayerSelectionSample extends Application {

  private MapView mapView;

  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // size the stage, add a title, and set scene to stage
      stage.setTitle("Feature Layer Selection Sample");

      // create a view for this ArcGISMap
      mapView = new MapView();

      // create a ArcGISMap with the streets basemap
      ArcGISMap map = new ArcGISMap(Basemap.createStreets());

      // set an initial viewpoint
      map.setInitialViewpoint(new Viewpoint(new Envelope(-1131596.019761, 3893114.069099, 3926705.982140,
          7977912.461790, 0, 0, SpatialReferences.getWebMercator())));

      // set the ArcGISMap to be displayed in the view

      // create feature layer with its service feature table
      // create the service feature table
      String damageAssessmentFeatureService = "";
      final ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(damageAssessmentFeatureService);

      // create the feature layer using the service feature table
      final FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
      featureLayer.setSelectionColor(0xFF00FFFF); // cyan color

      // add the layer to the ArcGISMap

      mapView.setOnMouseClicked(event -> {
        // check for primary or secondary mouse click
        if (event.isStillSincePress() && event.getButton() == MouseButton.PRIMARY) {
          // clear previous results

          // create a point from where the user clicked
          Point2D point = new Point2D(event.getX(), event.getY());

          // identify the clicked features
          final ListenableFuture<IdentifyLayerResult> results = mapView.identifyLayerAsync(featureLayer, point, 10,
              false, 10);
          results.addDoneListener(() -> {

            try {
              IdentifyLayerResult layer = results.get();

              // search the layers for identified features
              List<Feature> features = layer.getElements().stream().filter(g -> g instanceof Feature).map(
                  g -> (Feature) g).collect(Collectors.toList());

              // select features

            } catch (Exception e) {

      // add the map view to stack pane

    } catch (Exception e) {
      // on any error, display exception

   * Stops and releases all resources used in application.
  public void stop() {

    // release resources when the application closes
    if (mapView != null) {

   * Opens and runs application.
   * @param args arguments passed to this application
  public static void main(String[] args) {


In this topic
  1. How it works
  2. Code