Extent bookmarks

Download Sample Viewer

Description

This application demonstrates how to use the toolkit's JExtentBookmark to bookmark extents. The extent bookmarks are displayed in a panel on top of the map, which you can drag to reposition elsewhere.

Code snippet


    map = createMap();
    // add extent bookmarks
    JExtentBookmark extentBookmarks = new JExtentBookmark(map, "extent.bookmarks");
    extentBookmarks.setLocation(10, 10);
    // add components to application pane
    contentPane.add(extentBookmarks);
    contentPane.add(map);
  

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.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;

import com.esri.runtime.ArcGISRuntime;
import com.esri.toolkit.bookmarks.JExtentBookmark;
import com.esri.core.geometry.Envelope;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;

/**
 * This application demonstrates how to use the toolkit's {@link JExtentBookmark}
 * to bookmark extents. The extent bookmarks are displayed in a draggable panel
 * on top of the map.
 */
public class BookmarksApp {

  private static String FSP = System.getProperty("file.separator");

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

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public BookmarksApp() {

  }

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

    // map
    map = createMap();

    // add extent bookmarks
    JExtentBookmark extentBookmarks = new JExtentBookmark(map, getBookmarksFilePath());
    extentBookmarks.setLocation(10, 10);
    contentPane.add(extentBookmarks);

    // add some bookmarks if there are none
    if (extentBookmarks.getBookmarkCount() < 1) {
      extentBookmarks.addBookmark(
          "Boston", new Envelope(-7915906.2, 5211780.3, -7907660.4, 5217280.3));
      extentBookmarks.addBookmark(
          "Tokyo", new Envelope(15529071.2, 4234491.2, 15595037.4, 4278491.103));
      extentBookmarks.addBookmark(
          "Cape Town", new Envelope(2043409.3, -4025517.2, 2059901, -4014517.168));
      extentBookmarks.addBookmark(
          "London", new Envelope(-19830.3, 6705685.4, -3338.6, 6716685.423));
    }

    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
          BookmarksApp bookmarksApp = new BookmarksApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = bookmarksApp.createWindow();
          appWindow.add(bookmarksApp.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();
    jMap.setWrapAroundEnabled(true);

    // add a base layer
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_BASEMAP);
    jMap.getLayers().add(baseLayer);

    // set an initial map extent
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));

    return jMap;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Extent Bookmarks 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;
  }

  /**
   * Returns the absolute path of the bookmarks file.
   * If the user does not have permission to create a new file in current execution
   * directory, then the java.io.tmpdir will be used.
   * @return the absolute path to the bookmarks file.
   */
  private static String getBookmarksFilePath() {
    String relativeFilePath = "extent.bookmarks";
    String absoluteFilePath = null;
    // check if can create a file in the current execution directory.
    // can't use File.canWrite() because create is considered different from write. 
    try {
      File relativeFile = new File(relativeFilePath);
      try {
        relativeFile.createNewFile();
      } catch (Exception ex) {
        System.out.println("Failed to create bookmarks file at " + relativeFile.getAbsolutePath());
      }
      if (relativeFile.exists()) {
        absoluteFilePath = relativeFile.getAbsolutePath();
      } else {
        String tempDataDir = System.getProperty("java.io.tmpdir") + FSP + 
            "ArcGIS SDKs" + FSP + "java" + ArcGISRuntime.getAPIVersion() + 
            FSP + "sdk" + FSP + "samples" + FSP + "bookmarks";
        File tempFolder = new File(tempDataDir);
        if (!tempFolder.exists()) {
          tempFolder.mkdirs();
        }
        absoluteFilePath = tempDataDir + FSP + relativeFilePath;
        System.out.println("Bookmarks file will be created at " + absoluteFilePath);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return absoluteFilePath;
  }
}
Feedback on this topic?