WebMap charts

Download Sample Viewer

Description

In this sample, a WebMap housed via ArcGIS Online (http://www.arcgis.com) is displayed in a Java application. The graphics created online appear in the map and this application shows how to display an info-popup when a graphic is clicked, using the toolkit's InfopopupOverlay. The public web map used has charts displaying in its popups, and those charts can be seen in the Java's infopopups. The InfopopupOverlay source code is available and can be modified, for example to change what the 'zoom to' button does, such as zooming to a fixed resolution.

Code snippet


    private static final String MAP_ID = "1a40fa5cc1ab4569b79f45444d728067";
    JMap jMap = new JMap();
    WebMap webMap = null;
    try {
        webMap = WebMap.newInstance(MAP_ID, portal);
        jMap.loadWebMap(webMap);
    } catch (Exception e) {
        // handle exception
    }
  

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.core.portal.Portal;
import com.esri.core.portal.WebMap;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.GroupLayer;
import com.esri.map.JMap;
import com.esri.map.Layer;
import com.esri.map.LayerEvent;
import com.esri.map.LayerListEventListenerAdapter;
import com.esri.toolkit.overlays.InfoPopupOverlay;

/***
 * In this sample, a WebMap housed via ArcGIS Online (http://www.arcgis.com) is
 * displayed in a Java application.  The graphics created online appear in the map
 * and this application shows how to display a popup dialog when a graphic is clicked,
 * using the toolkit's InfoPopupOverlay. The public web map used has charts
 * displaying in its popups, and those charts can be seen in the Java's infopopups.
 * <p>
 * The InfoPopupOverlay source code is available and can be modified, for example
 * to change what the 'zoom to' button does, such as zooming to a fixed resolution. 
 * In this application the InfoPopupOverlay is constructed such that any layers 
 * added or removed from the map gets automatically added to the overlay; this is 
 * ideal for web maps where the layers in the map may not be known in advance.
 */
public class WebMapChartsApp {

  private JMap map;
  // arcgis.com portal (null credentials: anonymous access)
  private final Portal portal = new Portal("http://www.arcgis.com", null);
  // item ID of a public map on arcgis.com with charts
  private static final String MAP_ID = "1a40fa5cc1ab4569b79f45444d728067";

  public WebMapChartsApp() {
  }
  
  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the JMap, load the web map into the JMap.
   * @return a JMap
   */
  private JMap createMap() {

    final JMap jMap = new JMap();
    
    final InfoPopupOverlay popupOverlay = new InfoPopupOverlay();
    jMap.addMapOverlay(popupOverlay);
    
    // grab the ArcGISFeatureLayer when added to the map and associate it with the infopopup overlay
    jMap.getLayers().addLayerListEventListener(new LayerListEventListenerAdapter() {
      
      @Override
      public void multipleLayersAdded(LayerEvent event) {
        for (Layer layer : event.getChangedLayers().values()) {
          if (layer instanceof ArcGISFeatureLayer) {
            popupOverlay.addLayer(layer);
          }
          else if(layer instanceof GroupLayer) {
            for(Layer groupedLayer: ((GroupLayer) layer).getLayers()) {
              if(groupedLayer instanceof ArcGISFeatureLayer) {
                popupOverlay.addLayer(groupedLayer);
              }
            }
          }
        }
      }
      
      @Override
      public void layerAdded(LayerEvent event) {
        Layer layer = event.getChangedLayer();
        if (layer instanceof ArcGISFeatureLayer) {
          popupOverlay.addLayer(layer);
        } else if(layer instanceof GroupLayer) {
            for(Layer groupedLayer: ((GroupLayer) layer).getLayers()) {
              if(groupedLayer instanceof ArcGISFeatureLayer) {
                popupOverlay.addLayer(groupedLayer);
              }
            }
          }
         
      }
    });

    // create and load the web map
    WebMap webMap = null;
    try {
      webMap = WebMap.newInstance(MAP_ID, portal);
      jMap.loadWebMap(webMap);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return jMap;
  }

  // ------------------------------------------------------------------------
  // 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 {
          WebMapChartsApp webMapApp = new WebMapChartsApp();
          JFrame appWindow = webMapApp.createWindow();
          appWindow.add(webMapApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------

  public JComponent createUI() {
    // application content
    JComponent contentPane = createContentPane();

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

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

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(230, 80);
    JTextArea description = new JTextArea(
        "Click on one of the graphics to see an infopopup which displays charts " +
        "about the ecological footprint of nations.");
    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;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("WebMap Charts 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.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

}
Feedback on this topic?