Map navigation

Download Sample Viewer

Description

This application shows how to perform basic map navigation actions: panning, zooming, and map rotation.

Code snippet


    // pan to an extent
    map.panTo(extent);
    // zoom to the map's full extent
    map.zoomTo(map.getFullExtent());
    // zoom to the U.S.
    map.zoomTo(new Envelope(-15000000, 2000000, -7000000, 8000000));
    // set the map rotation
    map.setRotation(rotationAngle);
  

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.map;

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

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

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

/**
 * This application demonstrates how to programmatically pan, zoom,
 * and rotate the JMap.
 */
public class NavigationActionsApp {

  private JComponent contentPane;
  private JMap map;
  private final static int panDistance = 1000000; // in the base map's units (meters)
  private final static int rotationAngle = 10; // in degrees
  private final static int PANEL_WIDTH = 180;
  private int currentRotation = 0; // in degrees
  private final static String BASEMAP_URL =
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public NavigationActionsApp() {

  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   * Adds listeners to the buttons created in order to pan, zoom, and
   * rotate the map.
   */
  public JComponent createUI() throws Exception {
    // application content
    contentPane = createContentPane();

    // map
    map = createMap();

    // title
    JTextField txtTitle = new JTextField();
    txtTitle.setText("Navigation Actions");
    txtTitle.setHorizontalAlignment(SwingConstants.CENTER);
    txtTitle.setFont(new Font(txtTitle.getFont().getName(), txtTitle.getFont().getStyle(), 16));
    txtTitle.setMaximumSize(new Dimension(PANEL_WIDTH, 30));
    txtTitle.setBackground(Color.BLACK);
    txtTitle.setForeground(Color.WHITE);
    txtTitle.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));

    // button - when clicked, zoom to the U.S. extent
    JButton btnZoomUS = new JButton("Zoom to U.S.");
    btnZoomUS.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        map.zoomTo(new Envelope(-15000000, 2000000, -7000000, 8000000));
      }
    });
    btnZoomUS.setAlignmentX(Component.CENTER_ALIGNMENT);

    // button - when clicked, zoom to the full extent
    JButton btnZoomFullExtent = new JButton("Zoom to Full Extent");
    btnZoomFullExtent.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        map.zoomTo(map.getFullExtent());
      }
    });
    btnZoomFullExtent.setAlignmentX(Component.CENTER_ALIGNMENT);

    final JPanel extentPanel = new JPanel();
    extentPanel.setLayout(new BorderLayout());
    extentPanel.add(btnZoomUS, BorderLayout.NORTH);
    extentPanel.add(btnZoomFullExtent, BorderLayout.SOUTH);
    extentPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    extentPanel.setBackground(new Color(0, 0, 0, 100));

    // button - when clicked, pan West
    JButton btnPanLeft = new JButton("West");
    btnPanLeft.setPreferredSize(new Dimension(PANEL_WIDTH/2 - 18, 25));
    btnPanLeft.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Envelope extent = map.getExtent();
        extent.setXMin(extent.getXMin() - panDistance);
        map.panTo(extent);
      }
    });

    // button - when clicked, pan East
    JButton btnPanRight = new JButton("East");
    btnPanRight.setPreferredSize(new Dimension(PANEL_WIDTH/2 - 18, 25));
    btnPanRight.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Envelope extent = map.getExtent();
        extent.setXMin(extent.getXMin() + panDistance);
        map.panTo(extent);
      }
    });

    // button - when clicked, pan North
    JButton btnPanUp = new JButton("North");
    btnPanUp.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Envelope extent = map.getExtent();
        extent.setYMin(extent.getYMin() + panDistance);
        map.panTo(extent);
      }
    });

    // button - when clicked, pan South
    JButton btnPanDown = new JButton("South");
    btnPanDown.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Envelope extent = map.getExtent();
        extent.setYMin(extent.getYMin() - panDistance);
        map.panTo(extent);
      }
    });

    // group the panning UI items into a panel
    final JPanel panningPanel = new JPanel();
    panningPanel.setLayout(new BorderLayout());
    panningPanel.add(btnPanUp, BorderLayout.NORTH);
    panningPanel.add(btnPanLeft, BorderLayout.WEST);
    panningPanel.add(btnPanRight, BorderLayout.EAST);
    panningPanel.add(btnPanDown, BorderLayout.SOUTH);
    panningPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    panningPanel.setBackground(new Color(0, 0, 0, 100));

    // button - when clicked, rotate left
    JButton btnRotateLeft = new JButton("Rotate Left");
    btnRotateLeft.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        currentRotation = (currentRotation + rotationAngle) % 360;
        //System.out.println("Left to: " + currentRotation + " map at: " + map.getRotation());
        map.setRotation(currentRotation);
      }
    });

    // button - when clicked, rotate right
    JButton btnRotateRight = new JButton("Rotate Right");
    btnRotateRight.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        currentRotation = (currentRotation - rotationAngle);
        if (currentRotation < 0) {
          currentRotation = 360 + currentRotation;
        }
        //System.out.println("Right to: " + currentRotation + " map at: " + map.getRotation());
        map.setRotation(currentRotation);
      }
    });

    final JPanel rotationPanel = new JPanel();
    rotationPanel.setLayout(new BorderLayout());
    rotationPanel.add(btnRotateLeft, BorderLayout.NORTH);
    rotationPanel.add(btnRotateRight, BorderLayout.SOUTH);
    rotationPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    rotationPanel.setBackground(new Color(0, 0, 0, 100));

    // group the above UI items into a panel
    final JPanel controlPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(controlPanel, BoxLayout.Y_AXIS);
    controlPanel.setLayout(boxLayout);
    controlPanel.setLocation(10, 10);
    controlPanel.setSize(PANEL_WIDTH, 250);
    controlPanel.setBackground(new Color(0, 0, 0, 100));
    controlPanel.setBorder(new LineBorder(Color.BLACK, 3));

    controlPanel.add(txtTitle);
    controlPanel.add(Box.createVerticalStrut(5));
    controlPanel.add(extentPanel);
    controlPanel.add(Box.createVerticalStrut(10));
    controlPanel.add(panningPanel);
    controlPanel.add(Box.createVerticalStrut(10));
    controlPanel.add(rotationPanel);

    // add the panel to the main window
    contentPane.add(controlPanel);
    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
          NavigationActionsApp navigationActionsApp = new NavigationActionsApp();

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

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

  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();
    // -----------------------------------------------------------------------------------------
    // Base Layer - with U.S. topology, focus on U.S by default
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(BASEMAP_URL);
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
    jMap.getLayers().add(baseLayer);

    return jMap;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Map Navigation 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.setBounds(100, 100, 1000, 700);
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }
}
Feedback on this topic?