Switch overlays

Download Sample Viewer

Description

This application shows how to create simple map overlays and activate or deactivate them on button click. Three simple map overlays are created by extending the MapOverlay class. A logo overlay places a logo at the bottom left corner of the map; a graphic overlay adds a point graphic to the map on button click; and a zoom box overlay allows the user to zoom to a particular extent by creating a 'zoom box' to a particular area, using the mouse. To use the sample, click on one of the buttons to activate that overlay. Only one overlay will be active at a time.

Code snippet


    logoOverlay = new LogoOverlay();
    logoOverlay.setActive(false);
    map.addMapOverlay(logoOverlay);

    zoomOverlay = new ZoomBoxOverlay();
    zoomOverlay.setActive(false);
    map.addMapOverlay(zoomOverlay);

    graphicOverlay = new GraphicOverlay(graphicsLayer);
    graphicOverlay.setActive(false);
    map.addMapOverlay(graphicOverlay);
  

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.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
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 java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.GraphicsLayer;
import com.esri.map.JMap;

import com.esri.toolkit.overlays.ZoomBoxOverlay;
import com.esri.core.geometry.Envelope;
import com.esri.map.MapEventListenerAdapter;
import com.esri.map.MapEvent;
import javax.swing.JButton;
import javax.swing.border.LineBorder;

/***
 * Sample application for showing various simple overlays that can be written
 * by extending the MapOverlay class and by adding the overlays to the JMap
 * using <code>addMapOverlay</code>. The overlays can be activated or
 * deactivated using the <code>setActive</code> method. Care must be taken
 * when more than one overlay is active at the same time. This sample
 * application only allows for one active overlay at a time.
 */
public class OverlayDemoApp {

  private static final String URL_BASEMAP =
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";

  // components
  private JMap map;
  private GraphicsLayer graphicsLayer;
  private JButton logoButton;
  private JButton graphicButton;
  private JButton zoomButton;
  private ZoomBoxOverlay zoomOverlay;
  private LogoOverlay logoOverlay;
  private GraphicOverlay graphicOverlay;

  // UI constants
  private static final int BUTTON_WIDTH = 160;
  private static final int BUTTON_HEIGHT = 25;
  private static final Color BG_COLOR = new Color(0, 0, 0, 150);

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

    // application content
    JLayeredPane contentPane = createContentPane();

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

    // UI panel
    final JPanel buttonPanel = createButtonPanel();
    buttonPanel.setLocation(10, description.getHeight() + 20);
    contentPane.add(buttonPanel);

    // create map
    map = new JMap();

    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(URL_BASEMAP);
    map.getLayers().add(tiledLayer);

    // graphics layer for points added
    graphicsLayer = new GraphicsLayer();
    map.getLayers().add(graphicsLayer);

    // set map extent to USA
    map.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));

    map.addMapEventListener(new MapEventListenerAdapter() {
      @Override
      public void mapReady(final MapEvent event) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            // create overlays and enable all buttons
            onMapReady();
          }
        });
      }
    });

    contentPane.add(map);

    return contentPane;
  }

  /**
   * Method called on MapReady.
   * Zooms to the right extent, creates the overlays, activates the overlays,
   * and activates their corresponding buttons.
   */
  private void onMapReady() {

    logoOverlay = new LogoOverlay();
    logoOverlay.setActive(false);
    map.addMapOverlay(logoOverlay);

    zoomOverlay = new ZoomBoxOverlay();
    zoomOverlay.setActive(false);
    map.addMapOverlay(zoomOverlay);

    graphicOverlay = new GraphicOverlay(graphicsLayer);
    graphicOverlay.setActive(false);
    map.addMapOverlay(graphicOverlay);

    // activate buttons
    logoButton.setEnabled(true);
    graphicButton.setEnabled(true);
    zoomButton.setEnabled(true);
  }


  /**
   * Main method, launches the application.
   * @param args
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          OverlayDemoApp theApp = new OverlayDemoApp();
          JFrame appWindow = theApp.createWindow();
          appWindow.add(theApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  private JPanel createButtonPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setSize(BUTTON_WIDTH + 10, (BUTTON_HEIGHT*3) + (5*4));

    // buttons
    logoButton = new JButton("Logo Overlay");
    logoButton.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    logoButton.setMinimumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    logoButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        logoOverlay.setActive(true);
        logoButton.setEnabled(false);

        zoomOverlay.setActive(false);
        zoomButton.setEnabled(true);

        graphicOverlay.setActive(false);
        graphicButton.setEnabled(true);
      }
    });
    logoButton.setEnabled(false);

    graphicButton = new JButton("Graphic Overlay");
    graphicButton.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    graphicButton.setMinimumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    graphicButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        graphicOverlay.setActive(true);
        graphicButton.setEnabled(false);

        zoomOverlay.setActive(false);
        zoomButton.setEnabled(true);

        logoOverlay.setActive(false);
        logoButton.setEnabled(true);
      }
    });
    graphicButton.setEnabled(false);

    zoomButton = new JButton("Zoom Box Overlay");
    zoomButton.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    zoomButton.setMinimumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
    zoomButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        zoomOverlay.setActive(true);
        zoomButton.setEnabled(false);

        graphicOverlay.setActive(false);
        graphicButton.setEnabled(true);

        logoOverlay.setActive(false);
        logoButton.setEnabled(true);
      }
    });
    zoomButton.setEnabled(false);

    // layout all the components together into a panel
    panel.setBackground(BG_COLOR);
    panel.add(logoButton);
    panel.add(Box.createRigidArea(new Dimension(0,5)));
    panel.add(graphicButton);
    panel.add(Box.createRigidArea(new Dimension(0,5)));
    panel.add(zoomButton);
    panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    return panel;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(260, 175);
    JTextArea description = new JTextArea(
        "Click on a button to activate the corresponding overlay (only one overlay " +
            "will be active at a time).  The logo overlay displays a logo in the bottom left " +
            "corner of the map; the graphic overlay adds a point graphic on button click; " +
            "the zoom box overlay allows you to create a 'box' via a mouse click-and-drag " +
        "action and zooms to the box's extent when you release the button.");
    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,10,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 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;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Overlay Demo 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;
  }
}
/* 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.event.MouseEvent;
import javax.swing.SwingUtilities;
import com.esri.map.GraphicsLayer;
import com.esri.map.MapOverlay;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;

class GraphicOverlay extends MapOverlay {

  private static final long serialVersionUID = 1L;
  private GraphicsLayer graphicsLayer;
  private PictureMarkerSymbol symPoint;

  public GraphicOverlay(GraphicsLayer graphicsLayer) {
    this.graphicsLayer = graphicsLayer;
    symPoint = new PictureMarkerSymbol(
        "http://static.arcgis.com/images/Symbols/Basic/RedStickpin.png");
    symPoint.setSize(40, 40);
    symPoint.setOffsetY(20.0f);
  }

  @Override
  public void onMouseClicked(MouseEvent event) {

    if (SwingUtilities.isLeftMouseButton(event)) {
      // left-click
      if (event.getClickCount() == 1) {

        // get clicked point, convert to map point
        Point point = getMap().toMapPoint(event.getX(), event.getY());
        // show point graphic on map
        Graphic pointGraphic = new Graphic(point, symPoint);
        graphicsLayer.addGraphic(pointGraphic);
      }
    }
  }
}
/* 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.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import com.esri.map.MapOverlay;

/**
 * A map overlay to display a logo on the map, achieved by overriding the 
 * <code>onPaint</code> method.
 */
public class LogoOverlay extends MapOverlay {

    private static final long serialVersionUID = 1L;
    private static final int SPACING = 10;
    private static String pathToLogo = "resources/yourLogo.png";

    private BufferedImage bufferedImage;
    private int h;

    public LogoOverlay() {

        try {
            bufferedImage = ImageIO.read(this.getClass().getResource(pathToLogo));
            h = bufferedImage.getHeight(null);

        } catch (IOException e) {
            System.err.println("failed to create logo image");
            e.printStackTrace();
        }
    }

    @Override
    public void onPaint(Graphics graphics) {

        Graphics2D g = (Graphics2D) graphics;
        // places the logo at the bottom left of the screen, with spacing
        g.drawImage(bufferedImage, 0 + SPACING, getHeight() - h - SPACING, null);
        super.onPaint(graphics);
    }

}
Feedback on this topic?