Tiled map service layer

Download Sample Viewer

Description

This application shows how to load an ArcGIS Server tiled map service into a Java application. A default map service URL is shown which can be loaded by pressing on the 'Load Tiled Map Service' button. The user can load any other ArcGIS Server tiled map service similarly via its URL. Furthermore, the setWrapAroundEnabled method is called on the JMap, making the map wrap around the dateline for a smooth user experience. A directory of services is available at http://services.arcgisonline.com/ArcGIS/rest/services/.

Code snippet


    // create and add the tiled map service layer
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(layerUrl);
    map.getLayers().add(tiledLayer);
    // optionally, enable map wrap around
    map.setWrapAroundEnabled(true);
  

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

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
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.JMap;
import com.esri.map.Layer.LayerStatus;
import com.esri.map.LayerEvent;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerListEventListenerAdapter;

/**
 * This application shows how to load an ArcGIS Server tiled map service into a Java application.
 * A default map service URL is shown which can be loaded by pressing on the 'Load Tiled Map Service' button.
 * The user can load any other ArcGIS Server tiled map service similarly, by entering its URL and pressing the button.
 * A directory of services is available online at
 * <a href="http://services.arcgisonline.com/ArcGIS/rest/services/">http://services.arcgisonline.com/ArcGIS/rest/services/</a>.
 */
public class LoadTiledMapService {

  // default basemap: National Geographic world basemap
  private static final String DEFAULT_PATH =
      "http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer";

  private JMap map;
  private JTextField textField;
  private JPanel inputPanel;
  private JLayeredPane contentPane;
  private ArcGISTiledMapServiceLayer tiledLayer;

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public LoadTiledMapService() {
  }

  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------
  private void loadTiledMapService (String layerUrl) {

    tiledLayer = new ArcGISTiledMapServiceLayer(layerUrl);
    tiledLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent arg0) {
        if (arg0.getLayer().getStatus() != LayerStatus.INITIALIZED) {
          JOptionPane.showMessageDialog(contentPane.getParent(),
              wrap("Unable to load the tiled map service. " +
                  "Check that the URL is correct or try another tiled map service."),
                  "", JOptionPane.ERROR_MESSAGE);
        }
      }
    });
    map.getLayers().clear();
    map.getLayers().add(tiledLayer);
    map.setWrapAroundEnabled(true);
  }

  // ------------------------------------------------------------------------
  // 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 {
          LoadTiledMapService app = new LoadTiledMapService();
          JFrame appWindow = app.createWindow();
          appWindow.add(app.createUI());
          appWindow.setVisible(true);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------

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

    map = new JMap();

    contentPane = createContentPane();

    // input panel
    inputPanel = createPanel();
    inputPanel.setVisible(true);
    inputPanel.setLocation(10, 10);
    contentPane.add(inputPanel);

    map.getLayers().addLayerListEventListener(new LayerListEventListenerAdapter() {

      @Override
      public void layerAdded(LayerEvent arg0) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            map.setExtent(map.getFullExtent());
          }
        });
      }
    });

    contentPane.add(map);

    return contentPane;

  }

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

  private JButton createLoadButton() {
    JButton button = new JButton("Load Tiled Map Service");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        String layerUrl = textField.getText();
        // load the tiled map service entered by user
        loadTiledMapService(layerUrl);
      }
    });
    return button;
  }

  private JPanel createButtonPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    JButton loadButton = createLoadButton();
    panel.setBackground(new Color(0, 0, 0, 0));
    panel.add(loadButton);
    return panel;
  }

  /**
   * Creates a JPanel to obtain a URL from the user.
   * @return a JPanel with a text field and button
   */
  private JPanel createPanel() {
    JPanel idPanel = new JPanel();
    idPanel.setSize(550, 85);
    idPanel.setLayout(new BorderLayout(0, 5));
    idPanel.setBackground(new Color(0,0,0,120));
    idPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

    JLabel label = new JLabel("Enter the path to an ArcGIS tiled map service below:");
    label.setAlignmentX(Component.LEFT_ALIGNMENT);
    label.setForeground(Color.white);
    textField = new JTextField(DEFAULT_PATH);
    JPanel buttonPanel = createButtonPanel();

    idPanel.add(label, BorderLayout.NORTH);
    idPanel.add(textField, BorderLayout.CENTER);
    idPanel.add(buttonPanel, BorderLayout.SOUTH);
    idPanel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));

    return idPanel;
  }

  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Tiled Map Service 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.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?