WebMap infopopup

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 Infopopup when a graphic from a feature layer is clicked, using the toolkit's InfoPopupOverlay class.

Code snippet


  // create an infopopup overlay; true = automatically add any layers from the associated map
  InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay(true);
  infoPopupOverlay.setPopupTitle("Map Note");
  // add the overlay to the map
  jMap.addMapOverlay(infoPopupOverlay);

  // create and load the web map
  WebMap webMap = null;
  webMap = WebMap.newInstance(MAP_ID, portal);
  jMap.loadWebMap(webMap);
  

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 javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;

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

import com.esri.core.portal.Portal;
import com.esri.core.portal.WebMap;
import com.esri.map.JMap;

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 an info-popup when a graphic is clicked,
 * using the toolkit's InfoPopupOverlay.
 */
public class WebMapNotesApp {

  private JMap map;
  // arcgis.com portal (null credentials: anonymous access)
  private final Portal portal = new Portal("http://www.arcgis.com", null);
  // public web map with a 'Map Notes' layer created on arcgis.com
  private static final String MAP_ID = "f6c50f8c5304420985d72cf5088259bd";

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

    JMap jMap = new JMap();
    
    // create an infopopup overlay which automatically adds any layers added to the associated map
    InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay(true);
    infoPopupOverlay.setPopupTitle("Map Note");
    // add the overlay to the map
    jMap.addMapOverlay(infoPopupOverlay);

    // 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 {
          // instance of this application
          WebMapNotesApp webMapApp = new WebMapNotesApp();

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

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

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("WebMap Notes 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?