Skip To Content ArcGIS for Developers Sign In Dashboard

Overview map

Download Sample Viewer

Description

This sample application shows how to use the toolkit's OverviewMap. A JMap is created and added to the application pane and, separately, an OverviewMap is created and layered on top of the map. The OverviewMap is in this case created with the same base layer as the JMap, but it could be a different base layer with similar coverage (the spatial references do not need to match). As you pan and zoom the main JMap, the overview map will track the current extent and show it as a rectangle or a point. Panning and zooming is not possible in the overview map, but clicking on the overview map will re-centre the main map around the clicked point at the current scale.

Code snippet


    // create the OverviewMap
    ArcGISTiledMapServiceLayer overviewLayer = new ArcGISTiledMapServiceLayer(BASEMAP_URL);
    final OverviewMap overviewMap = new OverviewMap(map, overviewLayer);

    // some optional customization
    overviewMap.setMinimumFocusSize(8);
    overviewMap.setFocusPointSize(12);
  

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.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.OverviewMap;
import com.esri.core.geometry.Envelope;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;

/***
 * This sample application shows how to use the toolkit's
 * {@link OverviewMap}.  A JMap is created and added to the
 * application pane and, separately, an OverviewMap is created
 * and layered on top of the map.  The OverviewMap is in this
 * case created with the same base layer as the JMap, but it
 * could be a different base layer with similar coverage (the
 * spatial references do not need to match).
 * <p>
 * As you pan and zoom the main JMap, the overview map will
 * track the current extent and show it as a rectangle or
 * a point. Panning and zooming is not possible in the overview
 * map, but clicking on the overview map will re-centre the
 * main map around the clicked point at the current scale.
 */
public class OverviewMapApp {

  private JMap map;
  private OverviewMap overviewMap;
  private final static String BASEMAP_URL =
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  private final int OVERVIEW_MAP_SIZE = 256; //pixels

  //------------------------------------------------------------------------
  // Constructor
  //------------------------------------------------------------------------
  public OverviewMapApp() {
  }

  //------------------------------------------------------------------------
  // Core functionality
  //------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the JMap and OverviewMap.
   */
  public JComponent createUI() throws Exception {

    // application content
    JLayeredPane contentPane = createContentPane();

    // create the JMap
    map = createMap();

    // create the OverviewMap
    ArcGISTiledMapServiceLayer overviewLayer = new ArcGISTiledMapServiceLayer(BASEMAP_URL);
    overviewMap = new OverviewMap(map, overviewLayer);
    overviewMap.setSize(OVERVIEW_MAP_SIZE, OVERVIEW_MAP_SIZE);
    // some optional customization
    overviewMap.setMinimumFocusSize(8);
    overviewMap.setFocusPointSize(12);

    // title
    JLabel txtTitle = new JLabel("Overview Map");
    txtTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
    txtTitle.setFont(new Font("Dialog", Font.PLAIN, 12));
    txtTitle.setBackground(Color.BLACK);
    txtTitle.setForeground(Color.WHITE);

    // group title and buttons into a panel
    JPanel overviewMapPanel = new JPanel();
    overviewMapPanel.setLayout(new BoxLayout(overviewMapPanel, BoxLayout.Y_AXIS));
    overviewMapPanel.setLocation(10, 10);
    overviewMapPanel.setSize(OVERVIEW_MAP_SIZE, OVERVIEW_MAP_SIZE);
    overviewMapPanel.setBackground(Color.BLACK);
    overviewMapPanel.setBorder(new LineBorder(Color.DARK_GRAY, 2));
    overviewMapPanel.add(txtTitle);
    overviewMapPanel.add(overviewMap);

    // add the panel and map to the content pane
    contentPane.add(overviewMapPanel);
    contentPane.add(map);

    return contentPane;
  }

  //------------------------------------------------------------------------
  // 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
          OverviewMapApp app = new OverviewMapApp();

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

  //------------------------------------------------------------------------
  // Private methods
  //------------------------------------------------------------------------
  /**
   * Creates a map with a base layer.
   * @return a map.
   */
  private JMap createMap() throws Exception {

    final JMap jMap = new JMap();
    jMap.setWrapAroundEnabled(true);

    // Base Layer
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(BASEMAP_URL);
    jMap.getLayers().add(baseLayer);
    // set the map extent to the USA
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));

    return jMap;
  }

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