Tiled image service layer

Download Sample Viewer

Description

This sample shows how to display a tiled layer from an Image Service with a tile cache. An ArcGISTiledMapServiceLayer is created from the service URL, in the same way as with a MapServer URL.

Code snippet


    // the ImageServer used below has a tile cache
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Toronto/ImageServer");
    map.getLayers().add(tiledLayer);
  

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.BorderLayout;
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.SwingUtilities;

import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;

/**
 * This sample shows how to display a tiled layer from an Image Service that has a tile cache. 
 * An ArcGISTiledMapServiceLayer is created from the image service URL, in the same way as with 
 * a map service URL.
 */
public class ImageServiceAsTiledLayerApp {

  private JMap jMap;

  // Default constructor
  public ImageServiceAsTiledLayerApp() { }
  
  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the JMap and the tiled map service layer from the image service URL; the 
   * image service must have a tile cache for this to work.
   * 
   * @return a map
   */
  private JMap createMap() {

    final JMap map = new JMap();

    // create and add the tiled layer
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Toronto/ImageServer");
    map.getLayers().add(tiledLayer);

    return map;
  }

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

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

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

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

    // map
    jMap = createMap();

    contentPane.add(jMap);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Image Service as a Tiled Layer");
    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);
        jMap.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;
  }
}
Feedback on this topic?