OnDemand mode

Download Sample Viewer

Description

This sample application shows an ArcGISFeatureLayer in ON_DEMAND mode. Only features within the current map extent are retrieved. As the map extent changes, additional features are requested from the service. The service defines a maximum number of records (i.e. features) that can be returned.

The red points in the map are the active features from the Block Point feature layer. The blue points in the map are those graphics from the Census dynamic layer. Blue points are only visible when the feature layer has returned the maximum number of feature it can and there is no red point available to replace the blue point.

Pan to a new area to activate the features in the that extent. Zoom out about 3 levels (3 mouse wheel clicks or use keyboard down arrow 3 times). You should notice blue points appear from the dynamic layer. The feature layer has returned its maximum number of records. Zoom in - you should notice the blue points; red points replace the blue when the mouse is released. Left-click on an active feature to view attributes of that feature.

Code snippet


    // create ArcGISFeatureLayer from service URL
    final ArcGISFeatureLayer featureLayer = new ArcGISFeatureLayer(URL_BLOCK_POINTS);
    // ON_DEMAND mode will activate only those features in the current extent
    featureLayer.setOperationMode(QueryMode.ON_DEMAND);
    // define a renderer to symbolize active features
    featureLayer.setRenderer(new SimpleRenderer(SYM_CITY));
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.featurelayers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.overlays.InfoPopupOverlay;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISDynamicMapServiceLayer;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;
import com.esri.map.MapEvent;
import com.esri.map.MapEventListenerAdapter;
import com.esri.map.QueryMode;

/***
 * This sample application shows an ArcGISFeatureLayer in ON_DEMAND mode. Only features within 
 * the current map extent are retrieved. As the map extent changes, additional features are 
 * requested from the service. The service defines a maximum number of records (i.e. features) 
 * that can be returned.
 * <p>
 * The red points in the map are the active features from the Block Point feature layer. 
 * The blue points in the map are those graphics from the Census dynamic layer. 
 * Blue points are only visible when the feature layer has returned the maximum number of feature 
 * it can and there is no red point available to display over the blue point.
 * <p>
 * <li>Pan to a new area to activate the features in that extent.</li>
 * <li>Zoom out about 2 levels (2 mouse wheel clicks or use the keyboard down arrow 2 times). 
 * You should notice blue points appearing from the dynamic layer. The feature layer has returned 
 * its maximum number of records.</li>
 * <li>Zoom in - you should notice the blue points; red points replace the blue when the mouse is 
 * released.</li>
 * <li>Left-click on an active feature to view attributes of that feature.</li>
 */
public class OnDemandApp {

  // symbology
  final static SimpleMarkerSymbol SYM_CITY = new SimpleMarkerSymbol(Color.RED, 12, Style.CIRCLE);

  // resources
  final String URL_USA_TOPO = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
  final String URL_CENSUS   = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer";
  final String URL_BLOCK_POINTS = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0";

  // JMap
  private JMap map;

  // ------------------------------------------------------------------------
  // Constructors
  // ------------------------------------------------------------------------
  public OnDemandApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates an ArcGISFeatureLayer in OnDemand mode.
   */
  private ArcGISFeatureLayer createFeatureLayerInOnDemandMode() {
    final ArcGISFeatureLayer featureLayer = new ArcGISFeatureLayer(URL_BLOCK_POINTS);

    // OnDemand mode will activate only those features in the current extent.
    featureLayer.setOperationMode(QueryMode.ON_DEMAND);

    // define a renderer to symbolize active features.
    featureLayer.setRenderer(new SimpleRenderer(SYM_CITY));

    return featureLayer;
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          OnDemandApp onDemandApp = new OnDemandApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = onDemandApp.createWindow();
          appWindow.add(onDemandApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // application content
    JComponent contentPane = createContentPane();

    // map
    map = createMap();
    map.setSize(1000, 700);

    // description
    JPanel description = createDescription();
    contentPane.add(description);

    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("ArcGISFeatureLayer - On Demand Mode");
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setBounds(100, 100, 1000, 700);
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();
    // -----------------------------------------------------------------------------------------
    // Base Layer - World topographical map
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_USA_TOPO);
    jMap.addMapEventListener(new MapEventListenerAdapter() {
      @Override
      public void mapReady(final MapEvent arg0) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            // focus on Redlands, CA
            Point redlandsLatLong = new Point(-117.182236, 34.058917);
            // convert coordinate system from Lat-Long (WGS84, Wkid=4326) to the one
            // used by the base layer (Web Mercator, Wkid=102100)
            Point redlandsMercator = (Point) GeometryEngine.project(
                redlandsLatLong,
                SpatialReference.create(4326),
                baseLayer.getSpatialReference());
            Envelope envelope = new Envelope(redlandsMercator, 3000, 3000);
            ((JMap) arg0.getSource()).setExtent(envelope);
          }
        });
      }
    });
    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // -----------------------------------------------------------------------------------------
    // Base Layer - this has the census data
    // -----------------------------------------------------------------------------------------
    ArcGISDynamicMapServiceLayer censusLayer = new ArcGISDynamicMapServiceLayer(URL_CENSUS);
    layers.add(censusLayer);

    // -----------------------------------------------------------------------------------------
    // Feature Layer - demographics by block points
    // -----------------------------------------------------------------------------------------
    ArcGISFeatureLayer blockPointsLayer = createFeatureLayerInOnDemandMode();
    layers.add(blockPointsLayer);

    // -----------------------------------------------------------------------------------------
    // InfoPopupOverlay - a map overlay to display attributes of an active feature
    // -----------------------------------------------------------------------------------------
    final InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay();
    infoPopupOverlay.setPopupTitle("Feature");
    infoPopupOverlay.setItemTitle("Block: {BLOCK}");
    jMap.addMapOverlay(infoPopupOverlay);

    blockPointsLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener()
    {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent event)
      {
        //when the feature layer is initialized, let the InfoPopupOverlay know
        //so that the InfoPopupOverlay can show feature attributes in popups
        infoPopupOverlay.addLayer(event.getLayer());
      }
    });

    return jMap;
  }

  /**
   * Creates a description for this application.
   * @return a JPanel that has the description text.
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setOpaque(false);
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(360, 350);
    descriptionContainer.setLocation(10, 10);
    JTextArea description = new JTextArea(
        "The feature layer is in OnDemand mode. Only features within the current map extent " +
            "are retrieved.  As the map extent changes, additional features are requested." +
            "The service defines a maximum number of records (i.e. features) that can be " +
            "returned. \n\n The red points in the map are the active features from the " +
            "Block Point feature layer. The blue points in the map are " +
            "from the Census dynamic layer. Blue points are only visible when there is no red " +
            "point available to draw ontop of them, " +
            "since the feature layer has returned the maximum number of records.\n\n" +
            "- Pan to a new area to activate the features in that extent.\n" +
            "- Zoom out about 2 mouse wheel clicks; you should notice blue points appear " +
            "from the dynamic layer. The feature layer has returned its maximum number of " +
            "records.\n" +
            "- Zoom in; you should notice the blue points; red points replace the blue ones " +
            "when the mouse is released.\n" +
            "- Left-click on an active feature to view attributes of that feature."
        );
    description.setFont(new Font("Verdana", Font.PLAIN, 11));
    description.setForeground(Color.WHITE);
    description.setBackground(new Color(0, 0, 0, 180));
    description.setEditable(false);
    description.setLineWrap(true);
    description.setWrapStyleWord(true);
    description.setBorder(BorderFactory.createEmptyBorder(5,10,5,5));
    descriptionContainer.add(description);
    descriptionContainer.setBackground(new Color(0, 0, 0, 0));
    descriptionContainer.setBorder(new LineBorder(Color.BLACK, 3, false));
    return descriptionContainer;
  }
}
Feedback on this topic?