Scale bar

View on GitHubSample 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

ScaleBarSample.java
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright 2018 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.
 */

package com.esri.samples.scale_bar;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.UnitSystem;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.toolkit.Scalebar;

public class ScaleBarSample extends Application {

  private MapView mapView;

  @Override
  public void start(Stage stage) {

    // create stack pane and application scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);

    // set title, size and scene to stage
    stage.setTitle("Scale Bar Sample");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.setScene(scene);
    stage.show();

    // authentication with an API key or named user is required to access basemaps and other location services
    String yourAPIKey = System.getProperty("apiKey");
    ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

    // create a map with the standard imagery basemap style
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY_STANDARD);

    // create a map view and set the map to it
    mapView = new MapView();
    mapView.setMap(map);

    // set a viewpoint on the map view
    mapView.setViewpoint(new Viewpoint(64.1405, -16.2426, 9028));

    // create a scale bar for the map view
    Scalebar scaleBar = new Scalebar(mapView);

    // specify skin style for the scale bar
    scaleBar.setSkinStyle(Scalebar.SkinStyle.GRADUATED_LINE);

    // set the unit system (default is METRIC)
    scaleBar.setUnitSystem(UnitSystem.IMPERIAL);

    // to enhance visibility of the scale bar, by making background transparent
    Color transparentWhite = new Color(1, 1, 1, 0.7);
    scaleBar.setBackground(new Background(new BackgroundFill(transparentWhite, new CornerRadii(5), Insets.EMPTY)));

    // add the map view and scale bar to stack pane
    stackPane.getChildren().addAll(mapView, scaleBar);

    // set position of scale bar
    StackPane.setAlignment(scaleBar, Pos.BOTTOM_CENTER);
    // give padding to scale bar
    StackPane.setMargin(scaleBar, new Insets(0, 0, 50, 0));
  }

  // Stops and releases all resources used in application
  @Override
  public void stop() {
    if (mapView != null) {
      mapView.dispose();
    }
  }

  // Opens and runs application
  public static void main(String[] args) {
    Application.launch(args);
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.