Layer list

Download Sample Viewer

Description

Uses a layer list to control layer visibility and opacity.

Code snippet


    // initialize the components
    jMap = new JMap();
    jLayerList = new JLayerList(jMap);

    // create map and add the components
    contentPane.add(createMap(jMap), BorderLayout.CENTER);
    contentPane.add(jLayerList, 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.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

import com.esri.toolkit.JLayerList;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.map.ArcGISDynamicMapServiceLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.MapEvent;
import com.esri.map.MapEventListenerAdapter;
import com.esri.map.Layer;

/**
 * Uses a {@link JLayerList} to control layer visibility and opacity.
 */
public class LayerList {

  private JMap jMap;
  private JLayerList jLayerList;

  // Default constructor
  public LayerList() { }

  /**
   * 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
          LayerList layerListApp = new LayerList();

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

  public JComponent createUI() {

    JComponent contentPane = createContentPane();

    // initialize the components
    jMap = new JMap();
    jLayerList = new JLayerList(jMap);

    // create map and add the components
    contentPane.add(createMap(jMap), BorderLayout.CENTER);
    contentPane.add(jLayerList, BorderLayout.WEST);

    return contentPane;
  }

  class EditLayerName implements LayerInitializeCompleteListener {
    @Override
    public void layerInitializeComplete(final LayerInitializeCompleteEvent event) {
      SwingUtilities.invokeLater(new Runnable()
      {
        @Override
        public void run()
        {
          Layer layer = event.getLayer();

          // get the last part of the URL as a name for the layer e.g. get
          // "ESRI_StateCityHighway_USA" from
          // .../ESRI_StateCityHighway_USA/MapServer
          String[] words = layer.getUrl().split("/");
          layer.setName(words[words.length - 2]);

          // Update layer list
          jLayerList.refresh();
        }
      });
    }
  }

  class ZoomToLayer implements LayerInitializeCompleteListener {
    @Override
    public void layerInitializeComplete(
        final LayerInitializeCompleteEvent arg) {
      // layer is initialized
      jMap.addMapEventListener(new MapEventListenerAdapter() {
        @Override
        public void mapReady(MapEvent arg0) {
          // map is ready to zoom
          SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
              Point lowerLeft = (Point) GeometryEngine.project(
                  arg.getLayer().getFullExtent()
                  .getLowerLeft(), arg.getLayer()
                  .getDefaultSpatialReference(), jMap
                  .getSpatialReference());
              Point upperRight = (Point) GeometryEngine.project(
                  arg.getLayer().getFullExtent()
                  .getUpperRight(), arg.getLayer()
                  .getDefaultSpatialReference(), jMap
                  .getSpatialReference());

              jMap.zoomTo(new Envelope(lowerLeft.getX(),
                  lowerLeft.getY(), upperRight.getX(),
                  upperRight.getY()));
            }
          });
        }
      });
    }
  }

  /**
   * Creates the map for this application
   * @return a JMap
   */
  private JMap createMap(JMap map) {

    // add each layer, give it a name and refresh the layer list
    ArcGISTiledMapServiceLayer worldLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    worldLayer.addLayerInitializeCompleteListener(new EditLayerName());
    com.esri.map.LayerList layers = map.getLayers();
    layers.add(worldLayer);

    ArcGISDynamicMapServiceLayer highwayLayer = new ArcGISDynamicMapServiceLayer(
        "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer");
    highwayLayer.addLayerInitializeCompleteListener(new EditLayerName());
    layers.add(highwayLayer);

    ArcGISDynamicMapServiceLayer californiaLayer = new ArcGISDynamicMapServiceLayer(
        "http://maverick.arcgis.com/ArcGIS/rest/services/California/MapServer");
    californiaLayer.addLayerInitializeCompleteListener(new EditLayerName());
    // zoom to California layer
    californiaLayer.addLayerInitializeCompleteListener(new ZoomToLayer());
    layers.add(californiaLayer);

    return jMap;
  }

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