Local tiled layer

Download Sample Viewer

Description

This application shows how to add a local tiled layer (ArcGISLocalTiledLayer) into a map from a tile package (.tpk) file. Tile packages can be created using ArcGIS Desktop or from an ArcGIS map service supporting the creation of tile packages. Please refer to the ArcGIS Desktop Help for 'How to create a Tile Package', and to the 'Create an offline map' topic on the developers guide. Two check boxes are added to the user interface: one allowing the user to show or hide the Esri logo, and one to enable the map wrap around or disable it.

Code snippet


    // create and add the local tiled layer
    ArcGISLocalTiledLayer tiledLayer = new ArcGISLocalTiledLayer(
        "file path of .tpk");
    jMap.getLayers().add(tiledLayer);
    // optionally, enable map wrap around
    map.setWrapAroundEnabled(true);
    // show esri logo
    map.setShowingEsriLogo(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.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

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

import com.esri.client.local.ArcGISLocalTiledLayer;
import com.esri.core.geometry.Envelope;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.runtime.ArcGISRuntime;

/**
 * This application shows how to add a local tiled layer (<code>ArcGISLocalTiledLayer</code>)
 * into a map from a tile package (.tpk) file. Tile packages can be created using ArcGIS for 
 * Desktop or from an ArcGIS map service supporting the creation of tile packages. Please 
 * refer to the ArcGIS for Desktop Help for "How to create a Tile Package", and to the 
 * <a href="https://developers.arcgis.com/en/java/guide/create-an-offline-map.htm">Create 
 * an offline map</a> topic on the developers guide.
 * <p>
 * Two check boxes are added to the user interface: one allowing the user to show or hide the 
 * Esri logo, and one to enable the map wrap around or disable it.
 */
public class LocalTiledLayer {

  private JComponent contentPane;
  private JMap map;
  private static final String FSP = System.getProperty("file.separator");

  // Default constructor
  public LocalTiledLayer() {}

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          LocalTiledLayer localTiledLayerApp = new LocalTiledLayer();

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

  public JComponent createUI() {

    contentPane = createContentPane();

    // create map
    map = createMap();

    JPanel checkBoxPanel = new JPanel();
    checkBoxPanel.setLayout(new BorderLayout());
    checkBoxPanel.setLocation(10, 10);
    checkBoxPanel.setSize(140, 50);
    checkBoxPanel.setBackground(new Color(0, 0, 0, 0));

    // checkbox to control logo display
    final JCheckBox cbxEsriLogo = new JCheckBox("Show Esri logo");
    cbxEsriLogo.setSelected(map.isShowingEsriLogo());
    cbxEsriLogo.setBackground(Color.BLACK);
    cbxEsriLogo.setForeground(Color.WHITE);
    cbxEsriLogo.setFocusPainted(false);
    cbxEsriLogo.setFont(new Font("Dialog", Font.BOLD, 12));
    cbxEsriLogo.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          map.setShowingEsriLogo(true);
        } else {
          map.setShowingEsriLogo(false);
        }
      }
    });

    // checkbox to control map wrapping around the dateline
    final JCheckBox cbxWrapAround = new JCheckBox("Enable wrap around");
    cbxWrapAround.setSelected(map.isWrapAroundEnabled());
    cbxWrapAround.setBackground(Color.BLACK);
    cbxWrapAround.setForeground(Color.WHITE);
    cbxWrapAround.setFocusPainted(false);
    cbxWrapAround.setFont(new Font("Dialog", Font.BOLD, 12));
    cbxWrapAround.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          map.setWrapAroundEnabled(true);
        } else {
          map.setWrapAroundEnabled(false);
        }
      }
    });

    checkBoxPanel.add(cbxEsriLogo, BorderLayout.NORTH);
    checkBoxPanel.add(cbxWrapAround, BorderLayout.SOUTH);

    // add the map and check box panel to our content pane
    contentPane.add(checkBoxPanel);
    contentPane.add(map); // add map last

    return contentPane;
  }

  /**
   * Creates a map containing one local tiled layer.
   * @return a map.
   */
  private JMap createMap() {

    // create the jMap
    final JMap jMap = new JMap();

    // show Esri logo by default
    jMap.setShowingEsriLogo(true);

    // start with map wrap around not enabled
    jMap.setWrapAroundEnabled(false);

    // create a the local tiled layer from a tile package (.tpk)
    final ArcGISLocalTiledLayer tiledLayer =
        new ArcGISLocalTiledLayer(getPathSampleData() + "tpks" + FSP + "Topographic.tpk");
    // add the layer to the map
    jMap.getLayers().add(tiledLayer);
    
    tiledLayer
        .addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

          @Override
          public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
            if (e.getID() == LayerInitializeCompleteEvent.LOCALLAYERCREATE_ERROR) {
              String errMsg = "Failed to initialize due to "
                  + tiledLayer.getInitializationError();
              JOptionPane.showMessageDialog(jMap, wrap(errMsg), "",
                  JOptionPane.ERROR_MESSAGE);
            }
          }
        });

    // set an initial extent 
    jMap.setExtent(new Envelope(-19856505, -8827900, 18574809, 16806021));

    return jMap;
  }

  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 JFrame createWindow() {
    JFrame window = new JFrame("Local Tiled Layer 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;
  }

  private String getPathSampleData() {
    String dataPath = null;
    String javaPath = ArcGISRuntime.getInstallDirectory();
    if (javaPath != null) {
      if (!(javaPath.endsWith("/") || javaPath.endsWith("\\"))){
        javaPath += FSP;
      }
      dataPath = javaPath + "sdk" + FSP + "samples" + FSP + "data" + FSP;
    }
    File dataFile = new File(dataPath);
    if (!dataFile.exists()) { 
      dataPath = ".." + FSP + "data" + FSP;
    }
    return dataPath;
  }
  
  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?