WebMap

Download Sample Viewer

Description

This application shows how to display public web maps housed on the ArcGIS Online portal (http://www.arcgis.com) in a Java application. To use the sample, select a web map from the drop-down list and press the button to load the web map. The list features maps with KML, CSV, and OpenStreetMap layers.

Code snippet


    try {
        String newId = idMap.get(mapIds.getSelectedItem());
        WebMap webMap = WebMap.newInstance(newId, arcgisPortal);
        map.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.webmap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

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

/**
 * This application shows how to display public web maps housed on the ArcGIS Online 
 * portal (http://www.arcgis.com) in a Java application.
 * <p>
 * To use the sample, select a web map from the drop-down list and press the button 
 * to load the web map. The list features maps with KML, CSV, and OpenStreetMap layers.
 */
public class WebMapInputApp {

  private JLayeredPane contentPane;
  private JMap map;
  private JPanel inputPanel;
  private HashMap<String, String> idMap;
  private JComboBox<String> mapIds;
  // create the portal, using null credentials for anonymous access (view public maps only)
  private Portal arcgisPortal = new Portal("https://www.arcgis.com", null);

  // default constructor
  public WebMapInputApp() { }

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

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() {

    contentPane = createContentPane();

    // Create the map
    map = new JMap();

    // input panel
    inputPanel = createPanel();
    inputPanel.setVisible(true);
    inputPanel.setLocation(10, 10);

    // add to content pane
    contentPane.add(inputPanel);
    contentPane.add(map);

    return contentPane;

  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a panel for the user to select a web map to display.
   * @return a JPanel
   */
  private JPanel createPanel() {
    JPanel idPanel = new JPanel();
    idPanel.setSize(380, 3*5 + 3*25);
    idPanel.setLayout(new BoxLayout(idPanel, BoxLayout.Y_AXIS));
    idPanel.setBackground(new Color(0,0,0,100));

    JLabel label = new JLabel("Choose an ArcGIS Online web map below:");
    label.setForeground(Color.WHITE);

    String[] mapStrings = {
        "The true geography of the London Underground", 
        "Esri UK offices (KML, OpenStreetMap)", 
        "Earthquakes in the last 7 days (CSV)",
    "USA Tapestry Segmentation"};
    String[] idStrings = {
        "8a567ebac15748d39a747649a2e86cf4", 
        "f7095ca2ec924c7abe6d7b2fb98d0994", 
        "4c065d94a1f641e1b8ea4e0cd13ac683", 
    "4778fee6371d4e83a22786029f30c7e1"};
    idMap = new HashMap<>();
    for (int i = 0; i < idStrings.length; i++) {
      idMap.put(mapStrings[i], idStrings[i]);
    }

    mapIds = new JComboBox<>(mapStrings);
    mapIds.setSelectedIndex(0);
    mapIds.setAlignmentX(Component.LEFT_ALIGNMENT);

    JButton button = createButton();

    idPanel.add(label);
    idPanel.add(Box.createRigidArea(new Dimension(0,5)));
    idPanel.add(mapIds);
    idPanel.add(Box.createRigidArea(new Dimension(0,5)));
    idPanel.add(button);
    idPanel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));

    return idPanel;
  }

  private JButton createButton() {
    JButton button = new JButton("Load web map");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        try {
          String newId = idMap.get(mapIds.getSelectedItem());
          WebMap webMap = WebMap.newInstance(newId, arcgisPortal);
          map.loadWebMap(webMap);
        }
        catch (Exception e) {
          JOptionPane.showMessageDialog(contentPane.getParent(), 
              wrap("Unable to load the web map. " + e.getLocalizedMessage()), 
              "", 
              JOptionPane.ERROR_MESSAGE);
          e.printStackTrace();
        }
      }
    });
    return button;
  }

  /**
   * Creates a window. Disposes the map on application exit.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame();
    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);
        if (map != null) {
          map.dispose();
        }
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.setVisible(true);
    return contentPane;
  }
  
  private String wrap(String str) {
    // create a HTML string that wraps text when longer
    return "<html><p style='width:200px;'>" + str + "</html>";
  }
}
Feedback on this topic?