Infopopup overlay

Download Sample Viewer

Description

This shows how to use the infopopup overlay, an extension of a map overlay, to display feature information as an Infopopup.

Code snippet


    // create the infopopup overlay
    InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay();
    // customize the popup and item titles
    infoPopupOverlay.setPopupTitle("Feature");
    infoPopupOverlay.setItemTitle("Block: {BLOCK}");
    // add the layer of interest to the overlay
    infoPopupOverlay.addLayer(featureLayer);
    // add the overlay to the map
    jMap.addMapOverlay(infoPopupOverlay);
  

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

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.renderer.SimpleRenderer;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;
import com.esri.map.MapOptions;
import com.esri.map.MapOptions.MapType;

/**
 * This application demonstrates the use of the toolkit's {@link InfoPopupOverlay} to 
 * display attributes of a feature as a popup when that feature is clicked.
 * <p>
 * The features in the map represent census block points. An {@link InfoPopupOverlay} associated
 * to those features is added to the map as a map overlay.
 */
public class InfopopupOverlayApp {

  // JMap
  private JMap map;

  // symbology for feature layer
  private final static SimpleMarkerSymbol SYM_BLOCK_POINT =
      new SimpleMarkerSymbol(Color.RED, 12, Style.SQUARE);

  private final String URL_BLOCK_POINTS =
      "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/" +
          "ESRI_Census_USA/MapServer/0";
  // "BLOCK" is a field name in the layer
  private final String TITLE_TEMPLATE = "Block: {BLOCK}";

  // ------------------------------------------------------------------------
  // Default Constructor
  // ------------------------------------------------------------------------
  public InfopopupOverlayApp() { }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * 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 associate with the infopopup overlay - attributes of 
   * clicked features in this layer will be displayed in a popup dialog ('infopopup')
   * @param titleTemplate template to be used for the popup's item title
   */
  private void addInfopopupOverlay(JMap jMap, ArcGISFeatureLayer featureLayer, String titleTemplate) {
    // -----------------------------------------------------------------------------------------
    // InfoPopupOverlay - a map overlay to display attributes of a selected feature
    // -----------------------------------------------------------------------------------------

    // create an infopopup overlay
    InfoPopupOverlay infoPopupOverlay =
        new InfoPopupOverlay();
    // set the popup dialog title
    infoPopupOverlay.setPopupTitle("Feature");

    // set title to be used for selected items
    infoPopupOverlay.setItemTitle(titleTemplate);

    // associate the infopopup overlay to the feature layer
    infoPopupOverlay.addLayer(featureLayer);

    // add the infopopup overlay to the map
    jMap.addMapOverlay(infoPopupOverlay);
  }

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

          // 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();
        }
      }
    });
  }

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

    // description
    JPanel description = createDescription();
    description.setLocation(10, 10);
    contentPane.add(description);

    // map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    // focus on Redlands, California
    MapOptions mapOptions = new MapOptions(MapType.STREETS, 34.0514888762, -117.190346717, 14);
    final JMap jMap = new JMap(mapOptions);
    
    LayerList layers = jMap.getLayers();

    // -----------------------------------------------------------------------------------------
    // Feature Layer - demographics by block points
    // -----------------------------------------------------------------------------------------
    final ArcGISFeatureLayer blockPointsLayer = new ArcGISFeatureLayer(URL_BLOCK_POINTS);
    blockPointsLayer.setRenderer(new SimpleRenderer(SYM_BLOCK_POINT));
    blockPointsLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent arg0) {
        // add a infopopup overlay associated to the feature layer
        addInfopopupOverlay(jMap, blockPointsLayer, TITLE_TEMPLATE);
      }
    });
    layers.add(blockPointsLayer);

    return jMap;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Infopopup Overlay 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;
  }

  /**
   * 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 description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(210, 70);
    JTextArea description = new JTextArea(
        "Click on one of the census block points on the map to display its attributes" +
        " as an info-popup.");
    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,5,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?