WMS layer

Download Sample Viewer

Description

This sample shows how to display a WMS (Web Map Service) layer in a Java application. The WMS layer is displayed over a tiled map service layer, and constructed using a list of initial layers to display and initial styles for these layers.

Code snippet


    // set up the WMS layer creation parameters
    String url = "http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer";
    ArrayList<String> initialLayers = new ArrayList<String>(
        Arrays.asList(new String[] { "0", "1", "2" }));
    ArrayList<String> initialStyles = new ArrayList<String>(
        Arrays.asList(new String[] { "default", "default", "default" }));

    // create and add the WMS layer
    WmsDynamicMapServiceLayer wmsLayer =
        new WmsDynamicMapServiceLayer(url, initialLayers, initialStyles);
    jMap.getLayers().add(wmsLayer);
  

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

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.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.WmsDynamicMapServiceLayer;

/**
 * This sample shows how to display a WMS (Web Map Service) layer in a Java application.
 * The WMS layer is displayed over a tiled map service layer, and constructed using a
 * list of initial layers to display and initial styles for these layers.
 */
public class WMSLayer {

  JMap map;

  // Constructor
  public WMSLayer() { }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the JMap, adds the layers to display
   * @return a map
   */
  private JMap createMap() {

    final JMap jMap = new JMap();

    jMap.setExtent(new Envelope(-16145281, 2160905, -6537452, 8569385));

    // add a base layer
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    LayerList layers = jMap.getLayers();
    layers.add(tiledLayer);

    // set up the WMS layer creation parameters
    String url = "http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer";

    // create and add the WMS layer
    WmsDynamicMapServiceLayer wmsLayer =
        new WmsDynamicMapServiceLayer(url, new String[] { "0", "1", "2" });
    wmsLayer.setOpacity(0.7f);
    layers.add(wmsLayer);

    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
          WMSLayer wmsApp = new WMSLayer();

          // create the UI, including the map, for the application.
          JFrame appWindow = wmsApp.createWindow();
          appWindow.add(wmsApp.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("WMS 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);
        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?