View on GitHub Sample viewer app

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

Image of show 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 based on locale/optional settings.

How it works

  1. Create an ArcGISMap and add it to a MapView composable.
  2. Listen and track callback changes from onUnitsPerDipChanged, onViewpointChangedForCenterAndScale, onSpatialReferenceChanged using the composable MapView.
  3. Add the Scalebar composable positioned on top of the MapView
  4. Pass in the latest values of unitsPerDip, viewpoint, spatialReference and use a preferred maxWidth into the ScaleBar.

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.

This sample uses the Scalebar toolkit component. For information about setting up the toolkit, as well as code for the underlying component, visit the toolkit docs.

Tags

map, measure, scale, toolkit

Sample Code

MainActivity.kt MainActivity.kt ShowScaleBarViewModel.kt ShowScaleBarScreen.kt
/* Copyright 2025 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.arcgismaps.sample.showscalebar
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.showscalebar.screens.ShowScaleBarScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
enableEdgeToEdge()
setContent {
SampleAppTheme {
ShowScaleBarApp()
}
}
}
@Composable
private fun ShowScaleBarApp() {
Surface(color = MaterialTheme.colorScheme.background) {
ShowScaleBarScreen(
sampleName = getString(R.string.show_scale_bar_app_name)
)
}
}
}