Display grid

View inJavaKotlinView on GitHubSample viewer app

Display coordinate system grids including Latitude/Longitude, MGRS, UTM and USNG on a map view. Also, toggle label visibility and change the color of grid lines and grid labels.

Image of display grid

Use case

Grids are often used on printed maps, but can also be helpful on digital maps, to identify locations on a map.

How to use the sample

Tap on the 'Change Grid' button to open a settings view. You can select type of grid from 'Grid Type' (LatLong, MGRS, UTM and USNG) and modify its properties like label visibility, grid line color, and grid label color.

How it works

  1. Create an instance of one of the Grid types.
  2. Grid lines and labels can be styled per grid level with setLineSymbol(gridLevel, lineSymbol) and setTextSymbol(gridLevel, textSymbol) methods on the grid.
  3. The label position can be set with setLabelPosition(labelPosition) method on the grid.
  4. For the LatitudeLongitudeGrid type, you can specify a label format of DECIMAL_DEGREES or DEGREES_MINUTES_SECONDS.
  5. To set the grid, use the setGrid(grid) method on the map view.

Relevant API

  • Grid
  • LatitudeLongitudeGrid
  • MapView
  • MgrsGrid
  • SimpleLineSymbol
  • TextSymbol
  • UsngGrid
  • UtmGrid

Tags

coordinates, degrees, graticule, grid, latitude, longitude, MGRS, minutes, seconds, USNG, UTM

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 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.arcgisruntime.sample.displaygrid;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Grid;
import com.esri.arcgisruntime.mapping.view.LatitudeLongitudeGrid;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.MgrsGrid;
import com.esri.arcgisruntime.mapping.view.UsngGrid;
import com.esri.arcgisruntime.mapping.view.UtmGrid;
import com.esri.arcgisruntime.symbology.LineSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.TextSymbol;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private CheckBox mLabelsCheckBox;
  private int mLineColor;
  private int mLabelColor;

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

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // inflate views from activity_main
    mMapView = findViewById(R.id.mapView);
    Button mMenuButton = findViewById(R.id.menu_button);

    // set up a popup menu to manage grid settings
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final View view = getLayoutInflater().inflate(R.layout.popup_menu, null);
    builder.setView(view);
    final AlertDialog dialog = builder.create();

    // inflate views from popup_menu
    Spinner mGridSpinner = view.findViewById(R.id.layer_spinner);
    mLabelsCheckBox = view.findViewById(R.id.labels_checkBox);
    Spinner mColorsSpinner = view.findViewById(R.id.line_color_spinner);
    Spinner mLabelColorSpinner = view.findViewById(R.id.label_color_spinner);

    // create drop-down list of different grids
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
        getResources().getStringArray(R.array.layers_array));
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mGridSpinner.setAdapter(adapter);

    // create drop-down list of different colors
    ArrayAdapter<String> colorAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
        getResources().getStringArray(R.array.colors_array));
    colorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mColorsSpinner.setAdapter(colorAdapter);

    // create drop-down list of different label colors
    ArrayAdapter<String> labelColorAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
        getResources().getStringArray(R.array.colors_array));
    labelColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mLabelColorSpinner.setAdapter(labelColorAdapter);

    // create a map with imagery basemap
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);
    // set the map to be displayed in this view
    mMapView.setMap(map);
    // set viewpoint
    final Point center = new Point(-7702852.905619, 6217972.345771, 23227, SpatialReference.create(3857));
    mMapView.setViewpoint(new Viewpoint(center, 23227));

    // set defaults on grid
    mMapView.setGrid(new LatitudeLongitudeGrid());
    mLabelsCheckBox.setChecked(true);

    // change different grids on the Map View
    mGridSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // set the grid type
        switch (position) {
          case 0:
            mMapView.setGrid(new LatitudeLongitudeGrid());
            mMapView.setViewpointScaleAsync(23227);
            break;
          case 1:
            mMapView.setGrid(new MgrsGrid());
            mMapView.setViewpointScaleAsync(23227);
            break;
          case 2:
            mMapView.setGrid(new UtmGrid());
             mMapView.setViewpointScaleAsync(10000000);
            break;
          case 3:
            mMapView.setGrid(new UsngGrid());
            mMapView.setViewpointScaleAsync(23227);
            break;
          default:
            Toast.makeText(MainActivity.this,"Unsupported option", Toast.LENGTH_SHORT).show();
            break;
        }
        // make sure settings persist on grid type change
        setLabelVisibility();
        changeGridColor(mLineColor);
        changeLabelColor(mLabelColor);
      }

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

    // change grid lines color
    mColorsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //set the color
        switch (position) {
          case 0:
            mLineColor = Color.RED;
            break;
          case 1:
            mLineColor = Color.WHITE;
            break;
          case 2:
            mLineColor = Color.BLUE;
            break;
          default:
            Toast.makeText(MainActivity.this,"Unsupported option", Toast.LENGTH_SHORT).show();
            break;
        }
        changeGridColor(mLineColor);
      }

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

    // change grid labels color
    mLabelColorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // set the color
        switch (position) {
          case 0:
            mLabelColor = Color.RED;
            break;
          case 1:
            mLabelColor = Color.WHITE;
            break;
          case 2:
            mLabelColor = Color.BLUE;
            break;
          default:
            Toast.makeText(MainActivity.this,"Unsupported option", Toast.LENGTH_SHORT).show();
            break;
        }
        changeLabelColor(mLabelColor);
      }

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

    // hide and show label visibility when the checkbox is clicked
    mLabelsCheckBox.setOnClickListener(v -> setLabelVisibility());

    // display pop-up box when button is clicked
    mMenuButton.setOnClickListener(v -> dialog.show());
  }

  private void setLabelVisibility() {
    mMapView.getGrid().setLabelVisible(mLabelsCheckBox.isChecked());
  }

  private void changeGridColor(int color) {
    Grid grid = mMapView.getGrid();
    int gridLevels = grid.getLevelCount();
    for (int gridLevel = 0; gridLevel <= gridLevels - 1; gridLevel++) {
      LineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, color, gridLevel + 1);
      grid.setLineSymbol(gridLevel, lineSymbol);
    }
  }

  private void changeLabelColor(int color) {
    Grid grid = mMapView.getGrid();
    int gridLevels = grid.getLevelCount();
    for (int gridLevel = 0; gridLevel <= gridLevels - 1; gridLevel++) {
      TextSymbol textSymbol = new TextSymbol();
      textSymbol.setColor(color);
      textSymbol.setSize(14);
      textSymbol.setHorizontalAlignment(TextSymbol.HorizontalAlignment.LEFT);
      textSymbol.setVerticalAlignment(TextSymbol.VerticalAlignment.BOTTOM);
      textSymbol.setHaloColor(Color.WHITE);
      textSymbol.setHaloWidth(gridLevel + 1);
      grid.setTextSymbol(gridLevel, textSymbol);
    }
  }

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

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

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

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