Tiled map service layer

Download Sample Viewer


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);
    // optionally, enable map wrap around

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 =

  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() {

      public void layerInitializeComplete(LayerInitializeCompleteEvent arg0) {
        if (arg0.getLayer().getStatus() != LayerStatus.INITIALIZED) {
              wrap("Unable to load the tiled map service. " +
                  "Check that the URL is correct or try another tiled map service."),
                  "", JOptionPane.ERROR_MESSAGE);

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------

   * Starting point of this application.
   * @param args arguments to this application.
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
          LoadTiledMapService app = new LoadTiledMapService();
          JFrame appWindow = app.createWindow();
        catch (Exception e) {

  // ------------------------------------------------------------------------
  // 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.setLocation(10, 10);

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

      public void layerAdded(LayerEvent arg0) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {


    return contentPane;


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

  private JButton createLoadButton() {
    JButton button = new JButton("Load Tiled Map Service");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        String layerUrl = textField.getText();
        // load the tiled map service entered by user
    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));
    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));

    JLabel label = new JLabel("Enter the path to an ArcGIS tiled map service below:");
    textField = new JTextField(DEFAULT_PATH);
    JPanel buttonPanel = createButtonPanel();

    idPanel.add(label, BorderLayout.NORTH);
    idPanel.add(textField, BorderLayout.CENTER);
    idPanel.add(buttonPanel, BorderLayout.SOUTH);

    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.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent windowEvent) {
    return window;

   * Creates a content pane.
   * @return a content pane.
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout(0, 0));
    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?