WebMap bookmarks

Download Sample Viewer

Description

This application shows how to display the bookmarks of a web map in a Java application. The bookmarks were created on arcgis.com and get saved along with other info about the web map such as its layers, initial extent, and so on. When you create a WebMap in a Java application, the WebMap's bookmarks can be obtained using getBookmarks, then displayed in an appropriate Java UI component. Here we display the bookmarks in the toolkit component JExtentBookmark.

Code snippet


    // create the JExtentBookmark component
    extentBookmarks = new JExtentBookmark(map);

    // populate this component with the initialized webMap's bookmarks
    extentBookmarks.addBookmarks(webMap.getBookmarks());
  

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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

import com.esri.toolkit.bookmarks.JExtentBookmark;
import com.esri.core.portal.Portal;
import com.esri.core.portal.WebMap;
import com.esri.map.JMap;

/**
 * This application shows how to display the bookmarks of a web map in a Java 
 * application. The bookmarks were created on arcgis.com and get saved along 
 * with other info about the web map such as its layers, initial extent, and 
 * so on. When you create a WebMap in a Java application, the WebMap's 
 * bookmarks can be obtained using <code>getBookmarks</code>, then 
 * displayed in an appropriate Java UI component. Here we display the 
 * bookmarks in the toolkit component {@link JExtentBookmark}.
 */
public class WebMapBookmarksApp {

  private JMap map;
  private JExtentBookmark extentBookmarks;
  // ArcGIS.com portal instance, null for anonymous access
  private Portal portal = new Portal("https://www.arcgis.com", null);
  // public map with bookmarks, hosted on ArcGIS.com
  private static final String MAP_ID = "6bd9504d74734b2faa024ebd26b30b1f";

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------

  public JComponent createUI() {
    // container for our application content
    JComponent contentPane = createContentPane();

    map = new JMap();
    // create a WebMap
    WebMap webMap = null;
    try {
      webMap = WebMap.newInstance(MAP_ID, portal);
    } catch (Exception e) {
      JOptionPane.showMessageDialog(contentPane, 
          wrap("Error creating WebMap: " + e.getLocalizedMessage()), 
          "", JOptionPane.ERROR_MESSAGE);
      e.printStackTrace();
    }
    // load the JMap with the web map
    map.loadWebMap(webMap);

    // create extent bookmarks component
    extentBookmarks = new JExtentBookmark(map);
    extentBookmarks.setLocation(10, 10);
    extentBookmarks.setBackground(new Color(0, 0, 0, 120));
    extentBookmarks.setForeground(Color.WHITE);
    // get the bookmarks from the web map and add to bookmark UI component
    if (webMap!=null) extentBookmarks.addBookmarks(webMap.getBookmarks());

    contentPane.add(extentBookmarks);
    contentPane.add(map);

    return contentPane;
  }

  // default constructor
  public WebMapBookmarksApp() { }

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

          // create the application window and UI
          JFrame appWindow = app.createWindow();
          appWindow.add(app.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

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

  /**
   * Creates the application window. Disposes the map on application exit.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("WebMap 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);
        if (map != null) {
          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;
  }
  
  private String wrap(String str) {
    // create a HTML string that wraps text when longer
    return "<html><p style='width:200px;'>" + str + "</html>";
  }
}
Feedback on this topic?