Map reference scale

View on GitHubSample viewer app

Set the map's reference scale and which feature layers should honor the reference scale.

Image of map reference scale

Use case

Setting a reference scale on an ArcGISMap fixes the size of symbols and text to the desired height and width at that scale. As you zoom in and out, symbols and text will increase or decrease in size accordingly. When no reference scale is set, symbol and text sizes remain the same size relative to the MapView.

Map annotations are typically only relevant at certain scales. For instance, annotations to a map showing a construction site are only relevant at that construction site's scale. So, when the map is zoomed out that information shouldn't scale with the MapView, but should instead remain scaled with the ArcGISMap.

How to use the sample

  • Use the drop down at the top to set the map's reference scale (1:500,000 1:250,000 1:100,000 1:50,000).
  • Tap the button to set the map scale to the reference scale.
  • Use the checkboxes in the overflow menu to set which feature layers should honor the reference scale.

How it works

  1. Get and set the reference scale property on the ArcGISMap object.
  2. If checked, get and set the scale symbols property on each individual FeatureLayer object.

Relevant API

  • ArcGISMap
  • FeatureLayer

Additional information

The map reference scale should normally be set by the map's author and not exposed to the end user like it is in this sample.

Tags

map, reference scale, scene

Sample Code

MainActivity.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright 2019 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.arcgisruntime.mapreferencescale;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.LayerList;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private LayerList mOperationalLayers;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get web map as an ArcGISMap
    Portal portal = new Portal(getString(R.string.runtime_portal_url));
    PortalItem portalItem = new PortalItem(portal, getString(R.string.isle_of_wight_portal_item));
    ArcGISMap map = new ArcGISMap(portalItem);

    // get a reference to the map view and set the map to it
    mMapView = findViewById(R.id.mapView);
    mMapView.setMap(map);

    // get a reference to the spinner
    Spinner referenceScaleSpinner = findViewById(R.id.reference_scale_spinner);
    referenceScaleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        // get the reference scale from the spinner in the format "1:25,000"
        String referenceScaleString = String.valueOf(adapterView.getItemAtPosition(position));
        // use regex to get just the reference scale number as a string
        referenceScaleString = referenceScaleString.substring(referenceScaleString.indexOf(':') + 1)
            .replaceAll(",", "");
        // set the reference scale with the double value of the reference scale string
        setReferenceScale(Double.valueOf(referenceScaleString));
      }

      @Override public void onNothingSelected(AdapterView<?> adapterView) {
      }
    });

    // set initial selection to the 3rd option, 1:250,000
    referenceScaleSpinner.setSelection(2);

    // update map scale indicator on map scale change
    TextView mapScale = findViewById(R.id.currMapScaleTextView);
    mMapView.addMapScaleChangedListener(
        mapScaleChangedEvent -> mapScale.setText(String.valueOf(Math.round(mMapView.getMapScale()))));

    // use the current viewpoint's center and the current reference scale to set a new viewpoint
    Button matchScalesButton = findViewById(R.id.matchScalesButton);
    matchScalesButton.setOnClickListener(view -> mMapView.setViewpointAsync(new Viewpoint(
        mMapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE).getTargetGeometry().getExtent().getCenter(),
        mMapView.getMap().getReferenceScale()), 1));
  }

  /**
   * Set and get the map's reference scale.
   *
   * @param referenceScale as a double
   */
  private void setReferenceScale(double referenceScale) {
    mMapView.getMap().setReferenceScale(referenceScale);
  }

  /**
   * Set and get scale symbols for the given feature layer.
   *
   * @param featureLayer   that should honor scale symbols or not
   * @param isScaleSymbols true to honor reference scale, false to ignore reference scale
   */
  private void setScaleSymbol(FeatureLayer featureLayer, boolean isScaleSymbols) {
    featureLayer.setScaleSymbols(isScaleSymbols);
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    // once the map is loaded
    mMapView.getMap().addDoneLoadingListener(() -> {
      // get the map's operational layer list
      mOperationalLayers = mMapView.getMap().getOperationalLayers();
      // add each of those layers to the menu and set them to checked
      for (int i = 0; i < mOperationalLayers.size(); i++) {
        menu.add(Menu.NONE, i, Menu.NONE, mOperationalLayers.get(i).getName());
        menu.getItem(i).setCheckable(true);
        menu.getItem(i).setChecked(true);
      }
    });
    return super.onCreateOptionsMenu(menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    // toggle the checkbox of the menu item
    item.setChecked(!item.isChecked());
    // set the feature layer to honor the reference scale, or not
    setScaleSymbol((FeatureLayer) mOperationalLayers.get(item.getItemId()), item.isChecked());
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}

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