Scale bar
Download Sample ViewerDescription
This application demonstrates how to add a scale bar to a map using the ScaleBarOverlay toolkit component. The scale bar is a map overlay that should be added right after the map is initialized, in the mapReady listener.
Code snippet
// add a scale bar overlay to the map
ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay();
map.addMapOverlay(scaleBarOverlay);
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.toolkit;
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.toolkit.overlays.ScaleBarOverlay;
import com.esri.core.geometry.Envelope;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
/**
* This application demonstrates how to add a scale bar to a map using the
* {@link ScaleBarOverlay} toolkit component.
*/
public class ScaleBarApp {
// resources
final String URL_USA_TOPO =
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
// JMap
private JMap map;
// Constructor
public ScaleBarApp() { }
// ------------------------------------------------------------------------
// Core functionality
// ------------------------------------------------------------------------
/**
* Creates a map, adds a ScaleBarOverlay to the map.
* @return a map.
*/
private JMap createMap() throws Exception {
JMap jMap = new JMap();
// focus on U.S.
jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
// Base Layer
final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_USA_TOPO);
LayerList layers = jMap.getLayers();
layers.add(baseLayer);
// add a scale bar overlay
ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay();
jMap.addMapOverlay(scaleBarOverlay);
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
ScaleBarApp scaleBarApp = new ScaleBarApp();
// create the UI, including the map, for the application.
JFrame appWindow = scaleBarApp.createWindow();
appWindow.add(scaleBarApp.createUI());
appWindow.setVisible(true);
} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}
});
}
// ------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------
/**
* Creates and displays the UI, including the map, for this application.
* @return the UI component.
* @throws Exception on any error.
*/
public JComponent createUI() throws Exception {
// 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("Scale Bar 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;
}
}