OpenStreetMap custom layer

Download Sample Viewer

Description

This sample shows how to display a custom tiled layer adhering to the OpenStreetMap tile naming conventions using the OpenStreetMapLayer. The OpenStreetMapLayer constructor takes an ArrayList of tile server URLs, minimum and maximum zoom levels, and an attribution String, and a layer is created from these parameters. For more information, please see the OpenStreetMap wiki on http://wiki.openstreetmap.org/wiki/. For information on tile naming conventions, please see http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames.

Code snippet


  // set up parameters of your choice
  ArrayList<String> tileServerURLs = new ArrayList<String>();
  tileServerURLs.add("http://otile1.mqcdn.com/tiles/1.0.0/osm/");
  tileServerURLs.add("http://otile2.mqcdn.com/tiles/1.0.0/osm/");
  ...
  
  // create and add the OpenStreetMapLayer
  OpenStreetMapLayer osmLayer = new OpenStreetMapLayer(tileServerURLs, 0, 18, "attribution");
  jMap.getLayers().add(osmLayer);

  // if a key is required to use the tiles, use the setKey method on the layer
  //osmLayer.setKey("your key here");
  

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 java.util.ArrayList;

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.LocationOnMap;
import com.esri.map.OpenStreetMapLayer;

/***
 * This sample shows how to display a custom tiled layer adhering to the
 * OpenStreetMap tile naming conventions using the {@link OpenStreetMapLayer}.
 * The OpenStreetMapLayer constructor takes an ArrayList of tile server
 * URLs, minimum and maximum zoom levels, and an attribution String,
 * and a layer is created from these parameters.
 * <p>
 * For more information, please see the OpenStreetMap wiki on
 * http://wiki.openstreetmap.org/wiki/ . For information on tile
 * naming conventions, please see
 * http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames .
 */
public class OpenStreetMapCustomApp {

  private JMap map;

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

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

    final JMap jMap = new JMap();

    // optionally change the copyright location
    jMap.setCopyrightLocation(LocationOnMap.TOP_LEFT);

    // set up parameters of your choice;
    // Map Quest tile servers and attribution, in this case
    ArrayList<String> tileServerURLs = new ArrayList<>();
    tileServerURLs.add("http://otile1.mqcdn.com/tiles/1.0.0/osm/");
    tileServerURLs.add("http://otile2.mqcdn.com/tiles/1.0.0/osm/");
    tileServerURLs.add("http://otile3.mqcdn.com/tiles/1.0.0/osm/");
    tileServerURLs.add("http://otile4.mqcdn.com/tiles/1.0.0/osm/");
    String attribution = "Data, imagery and map information provided by " +
        "<a href=\"http://www.mapquest.com\">MapQuest</a>, " +
        "<a href=\"http://www.openstreetmap.org\">OpenStreetMap.org</a> " +
        "and contributors, " +
        "<a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CCBYSA</a>";

    // create and add the OpenStreetMapLayer
    OpenStreetMapLayer osmLayer = new OpenStreetMapLayer(tileServerURLs, 0, 18, attribution);
    jMap.getLayers().add(osmLayer);
    // if a key is required to use the tiles, use the setKey method on the layer
    //osmLayer.setKey("your key here");

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

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