Navigator

Download Sample Viewer

Description

This sample demonstrates the use of the NavigatorOverlay class to add a navigation control UI element to the map. The navigator control allows panning and zooming, as well as map rotation.

Code snippet


    // add a navigator overlay to the map
    NavigatorOverlay navigatorOverlay = new NavigatorOverlay();
    jMap.addMapOverlay(navigatorOverlay);
  

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

import com.esri.toolkit.overlays.NavigatorOverlay;
import com.esri.core.geometry.Envelope;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.LocationOnMap;

/**
 * This application shows how to add a navigation control to a map using 
 * the {@link NavigatorOverlay}. The navigation control is a map overlay that 
 * should be added to the map using <code>addMapOverlay</code> after the JMap 
 * is created. The location of the navigation control can be set using 
 * <code>setLocation(LocationOnMap)</code>.
 */
public class NavigatorOverlayApp {

  /* Default constructor */
  public NavigatorOverlayApp() { }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the JMap, adding a basemap and the navigator overlay.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();
    jMap.setAnimationDuration(0.5f);
    // -----------------------------------------------------------------------------------------
    // Base Layer
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer");
    // focus on U.S.
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    NavigatorOverlay navigatorOverlay = new NavigatorOverlay();
    navigatorOverlay.setLocation(LocationOnMap.TOP_LEFT);
    jMap.addMapOverlay(navigatorOverlay);

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

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

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // application content
    JComponent contentPane = createContentPane();

    // map
    JMap jMap = createMap();
    contentPane.add(jMap);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private static 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));
    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?