OpenStreetMap layer

Download Sample Viewer

Description

This sample shows how to display an OpenStreetMap layer in a map. The empty OpenStreetMapLayer constructor is used which results in the default tile servers, corresponding to the 'Standard' (or 'Mapnik') style on www.openstreetmap.org.

Code snippet


  // create and add the OpenStreetMapLayer
  OpenStreetMapLayer osmLayer = new OpenStreetMapLayer();
  jMap.getLayers().add(osmLayer); 
  

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.core.geometry.Envelope;
import com.esri.map.JMap;
import com.esri.map.OpenStreetMapLayer;

/**
 * This sample shows how to display a {@link OpenStreetMapLayer} in a map.
 * The empty OpenStreetMapLayer constructor is used which results in
 * the default tile servers, correpsonding to the 'Standard' (or 'Mapnik')
 * style on www.openstreetmap.org.
 */
public class OpenStreetMapLayerApp {

  private JMap map;

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public OpenStreetMapLayerApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates the OpenStreetMap layer and adds it to the map.
   * @return a map.
   */
  private JMap createMap() {

    JMap jMap = new JMap();

    // create and add the OpenStreetMapLayer
    OpenStreetMapLayer osmLayer = new OpenStreetMapLayer();
    jMap.getLayers().add(osmLayer);

    // set the extent
    jMap.setExtent(new Envelope(-19856505, -8827900, 18574809, 16806021));
    return jMap;
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  public JComponent createUI() {

    // create the content pane
    JLayeredPane contentPane = createContentPane();

    // create the map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // 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
          OpenStreetMapLayerApp osmApp = new OpenStreetMapLayerApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = osmApp.createWindow();
          appWindow.add(osmApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

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

  /**
   * 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?