Legend

Download Sample Viewer

Description

This application shows how to use the toolkit's JLegend to add a legend to a map. In the map, a feature layer containing earthquake data is shown above a dynamic layer showing the population density across the United States. A tiled map service is used as the base layer. These 3 layers automatically appear in the legend once the layers are initialized. In addition, an Infopopup Overlay is added to this map which displays information on the earthquake when you click on an earthquake graphic (represented by coloured triangles).

Code snippet


    // create the map
    map = createMap();
    // create the legend, using the map created
    JLegend legend = new JLegend(map);
    legend.setPreferredSize(new Dimension(250, 700));
    // add the map and legend to the application pane
    contentPane.add(map, BorderLayout.CENTER);
    contentPane.add(legend, BorderLayout.WEST);
  

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

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

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.legend.JLegend;
import com.esri.toolkit.overlays.InfoPopupOverlay;
import com.esri.core.geometry.Envelope;
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;

/**
 * This application shows how to use the toolkit's {@link JLegend} to
 * add a legend to a map.  In the map, a feature layer containing
 * earthquake data is shown above a dynamic layer showing the population
 * density across the United States.  A tiled map service is used as
 * the base layer.  These 3 layers automatically appear in the legend
 * once the layers are initialized.
 * <p>
 * In addition, an infopopup overlay is added to this map which
 * displays information on the earthquake when you click on an
 * earthquake graphic (represented by coloured triangles).
 */
public class LegendApp {

  // JMap
  private JMap map;

  // resources
  private static final String URL_BASE_LAYER =
      "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  private static final String URL_USA_POP_DENSITY =
      "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Population_Density/MapServer";
  private static final String URL_EARTHQUAKES =
      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0";

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public LegendApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------

  /**
   * Creates and displays the UI, including the map and legend.
   */
  public JComponent createUI() throws Exception {

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout(0, 5));

    // create the map
    map = createMap();
    // create the legend, using the map created
    JLegend legend = new JLegend(map);
    legend.setPreferredSize(new Dimension(250, 700));
    legend.setBorder(new LineBorder(new Color(205, 205, 255), 3));

    // add the components to the application pane
    contentPane.add(map, BorderLayout.CENTER);
    contentPane.add(legend, BorderLayout.WEST);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // 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
          LegendApp infopopupOverlayApp = new LegendApp();

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

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();

    // Base Layer
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_BASE_LAYER);
    // zoom to the U.S.
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // Dynamic layer
    final ArcGISDynamicMapServiceLayer dynamicLayer = new ArcGISDynamicMapServiceLayer(URL_USA_POP_DENSITY);
    layers.add(dynamicLayer);

    // Feature Layer
    final ArcGISFeatureLayer earthquakesLayer = new ArcGISFeatureLayer(URL_EARTHQUAKES);
    earthquakesLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent arg0) {
        // add a infopopup overlay associated to the feature layer
        addInfopopupOverlay(jMap, earthquakesLayer);
      }
    });
    layers.add(earthquakesLayer);

    return jMap;
  }

  /**
   * Adds an infopopup overlay.
   * @param jMap map to which the infopopup overlay will be added to as a map overlay.
   * @param featureLayer feature layer to which the infopopup will be associated to. The
   * attributes of clicked features in this layer will be displayed in popups.
   */
  private void addInfopopupOverlay(JMap jMap, ArcGISFeatureLayer featureLayer) {
    final InfoPopupOverlay infoPopupOverlay =
        new InfoPopupOverlay();
    infoPopupOverlay.setPopupTitle("Earthquake");
    infoPopupOverlay.setItemTitle("Name: {Name}");
    infoPopupOverlay.addLayer(featureLayer);
    jMap.addMapOverlay(infoPopupOverlay);
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Legend 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;
  }
}
Feedback on this topic?