Local Server feature layer

Download Sample Viewer

Description

This sample shows how to display a local feature layer using the Local Server. The tiled layer constrains the minimum and maximum zoom of the map. The default renderer of the feature layer is overridden with a new SimpleMarkerSymbol. The maximum number of records returned from the local feature service is set before the layer is initialized, using the setMaximumRecords method.

Code snippet


    // create feature layer with Cities feature class
    final ArcGISLocalFeatureLayer citiesLayer = new ArcGISLocalFeatureLayer(
        "file path of .mpk", "Cities");
    // set maximum records if desired (local only), default is 1000
    citiesLayer.setMaximumRecords(3000);
    map.getLayers().add(citiesLayer);

    // override default rendering option
    SimpleRenderer citiesRenderer =
        new SimpleRenderer(new SimpleMarkerSymbol(new Color(220, 0, 0), 8, Style.CIRCLE));
    citiesLayer.setRenderer(citiesRenderer);
  

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.localserver;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import com.esri.client.local.ArcGISLocalFeatureLayer;
import com.esri.client.local.ArcGISLocalTiledLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;
import com.esri.runtime.ArcGISRuntime;

/**
 * This sample shows how to display a local feature layer over a local tiled layer.
 * The tiled layer constrains the minimum and maximum zoom of the map.
 * The default renderer of the feature layer is overridden with a new SimpleMarkerSymbol.
 * The maximum numbers of records returned from the local feature service is set
 * before the layer is initialized, using <code>setMaximumRecords</code>.
 *
 */
public class LocalSimpleFeatureLayer {

  private JComponent contentPane;
  private JMap map;
  private JProgressBar progressBar;
  private static String FSP = System.getProperty("file.separator");

  public LocalSimpleFeatureLayer() {

  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          LocalSimpleFeatureLayer app = new LocalSimpleFeatureLayer();

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

  public JComponent createUI() {

    contentPane = createContentPane();

    // progress bar
    progressBar = createProgressBar(contentPane);

    // create map
    map = createMap();

    contentPane.add(progressBar);
    contentPane.add(map);

    return contentPane;
  }

  /**
   * Creates the map.
   * @return a map.
   */
  private JMap createMap() {

    final JMap jMap = new JMap();

    // setting a tiled layer as basemap
    ArcGISLocalTiledLayer tiledLayer = new ArcGISLocalTiledLayer(
        getPathSampleData() + "tpks" + FSP + "Topographic.tpk");
    final LayerList layers = jMap.getLayers();
    layers.add(tiledLayer);

    updateProgressBarUI("Starting a map service for a feature layer...", true);

    // create feature layer with Cities feature class
    final ArcGISLocalFeatureLayer citiesLayer = new ArcGISLocalFeatureLayer(
        getPathSampleData() + "mpks" + FSP + "USCitiesStates.mpk", "Cities");
    citiesLayer
        .addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
          @Override
          public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
            synchronized (progressBar) {
              if (e.getID() == LayerInitializeCompleteEvent.LOCALLAYERCREATE_ERROR) {
                String errMsg = "Failed to initialize due to "
                    + citiesLayer.getInitializationError();
                JOptionPane.showMessageDialog(jMap, wrap(errMsg), "",
                    JOptionPane.ERROR_MESSAGE);
              }
              updateProgressBarUI(null, false);
            }
          }
        });
    // set an opacity if desired
    //citiesLayer.setOpacity(0.8f);
    // set maximum records if desired (local only), default is 1000
    // this must be done before the layer is initialized
    citiesLayer.setMaximumRecords(3000);
    layers.add(citiesLayer);

    // override default rendering option
    SimpleRenderer citiesRenderer =
        new SimpleRenderer(new SimpleMarkerSymbol(new Color(220, 0, 0), 8, Style.CIRCLE));
    citiesLayer.setRenderer(citiesRenderer);

    // zooms to mainland US
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));

    return jMap;
  }

  /**
   * 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 progress bar.
   * @param parent progress bar's parent. The horizontal axis of the progress bar will be
   * center-aligned to the parent.
   * @return a progress bar.
   */
  private static JProgressBar createProgressBar(final JComponent parent) {
    final JProgressBar progressBar = new JProgressBar();
    progressBar.setSize(350, 20);
    parent.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        progressBar.setLocation(
            parent.getWidth()/2 - progressBar.getWidth()/2,
            parent.getHeight() - progressBar.getHeight() - 20);
      }
    });
    progressBar.setStringPainted(true);
    progressBar.setIndeterminate(true);
    return progressBar;
  }

  /**
   * Updates progress bar UI from the Swing's Event Dispatch Thread.
   * @param str string to be set.
   * @param visible flag to indicate visibility of the progress bar.
   */
  private void updateProgressBarUI(final String str, final boolean visible) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        if (str != null) {
          progressBar.setString(str);
        }
        progressBar.setVisible(visible);
      }
    }); 
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Local Simple Feature Layer Application");
    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;
  }

  private String getPathSampleData() {
    String dataPath = null;
    String javaPath = ArcGISRuntime.getInstallDirectory();
    if (javaPath != null) {
      if (!(javaPath.endsWith("/") || javaPath.endsWith("\\"))){
        javaPath += FSP;
      }
      dataPath = javaPath + "sdk" + FSP + "samples" + FSP + "data" + FSP;
    }
    File dataFile = new File(dataPath);
    if (!dataFile.exists()) { 
      dataPath = ".." + FSP + "data" + FSP;
    }
    return dataPath;
  }
  
  private String wrap(String str) {
    // create a HTML string that wraps text when longer
    return "<html><p style='width:200px;'>" + str + "</html>";
  }
}
Feedback on this topic?