View on GitHub Sample viewer app

Add a scale bar to visually gauge distances on a map.

Image of scale bar

Use case

Allows a user to have a visual reference for distances when navigating the map.

How to use the sample

Zoom in or out of the map. The scale bar will automatically display the appropriate scale based on zoom level. Units can be in metric and/or imperial.

How it works

  1. Create an ArcGISMap and display it in a MapView.
  2. Create a ScaleBar passing in the map view.
  3. Set the scale bar’s skin style with setSkinStyle(skinStyle).
  4. Set the unit system with setUnitSystem(unitSystem).
  5. Add the scale bar to the UI overlaying the map view.

Relevant API

  • ArcGISMap
  • MapView
  • Scalebar
  • UnitSystem

Additional information

The scale will be accurate for the center of the map, and in general more accurate at larger scales (zoomed in). This means at smaller scales (zoomed out), the reading may be inaccurate at the extremes of the visible extent.

Tags

map, measure, scale, toolkit

Sample Code

module-info.java module-info.java ScaleBarSample.java
/*
* Copyright 2022 Esri.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module com.esri.samples.scale_bar {
// require ArcGIS Maps SDK for Java module
requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder
requires org.slf4j.nop;
// require JavaFX modules that the application uses
requires javafx.graphics;
// require ArcGIS Maps SDK for Java Toolkit
requires com.esri.arcgisruntime.toolkit;
exports com.esri.samples.scale_bar;
}