KML layer

Download Sample Viewer

Description

This sample application shows how to add a KMLLayer to a map. The KMLLayer constructor takes the URL of the KML or KMZ (compressed KML) file, as well as a spatial reference to assign to the layer. This should be your basemap's spatial reference.

Code snippet


    // add KML layer from publicly accessible URL and spatial reference (the tiled layer's)
  final KMLLayer kmlLayer = new KMLLayer(
    "https://developers.google.com/kml/documentation/KML_Samples.kml", 
    SpatialReference.create(102100));
  layers.add(kmlLayer);
  

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

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.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.KMLLayer;

/***
 * This sample application shows how to add a {@link KMLLayer} to a map.
 * The KMLLayer constructor takes the URL of the KML or KMZ (compressed KML)
 * file, as well as a spatial reference to assign to the layer. This should
 * be your basemap's spatial reference.
 */
public class KmlLayerApp {

  private JMap map;

  //------------------------------------------------------------------------
  // Constructor
  //------------------------------------------------------------------------
  public KmlLayerApp() {

  }

  //------------------------------------------------------------------------
  // Core Functionality
  //------------------------------------------------------------------------
  /**
   * Creates the JMap, adds a tiled layer then a KMLLayer to the map
   * @return a map
   */
  private JMap createMap() {

    final JMap jMap = new JMap();

    // set default extent 
    jMap.setExtent(new Envelope(-13590847, 4497767, -13589934, 4498377.41));

    LayerList layers = jMap.getLayers();

    // add a tiled layer basemap
    final ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    layers.add(tiledLayer);

    // add KML layer from publicly accessible URL and spatial reference (the tiled layer's)
    final KMLLayer kmlLayer = new KMLLayer(
        "https://developers.google.com/kml/documentation/KML_Samples.kml",
        SpatialReference.create(102100));
    layers.add(kmlLayer);

    return jMap;
  }

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

          // 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
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

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