Display a grid

View inC++QMLView 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.

screenshot

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 in the toolbar 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 DecimalDegrees or DegreesMinutesSeconds.
  5. To set the grid, use the setGrid(grid) method on the map view.

Relevant API

  • ArcGISGrid
  • LatitudeLongitudeGrid
  • LatitudeLongitudeGridLabelFormat::DecimalDegrees
  • LatitudeLongitudeGridLabelFormat::DegreesMinutesSeconds
  • MapView
  • MGRSGrid
  • SimpleLineSymbol
  • TextSymbol
  • USNGGrid
  • UTMGrid

Tags

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

Sample Code

DisplayGrid.cppDisplayGrid.cppDisplayGrid.hDisplayGrid.qml
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// [WriteFile Name=DisplayGrid, Category=DisplayInformation]
// [Legal]
// Copyright 2016 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "DisplayGrid.h"

#include "Map.h"
#include "MapQuickView.h"
#include "Grid.h"
#include "LatitudeLongitudeGrid.h"
#include "UTMGrid.h"
#include "USNGGrid.h"
#include "MGRSGrid.h"
#include "SimpleLineSymbol.h"
#include "TextSymbol.h"
#include "Viewpoint.h"
#include "Point.h"

using namespace Esri::ArcGISRuntime;

const QString DisplayGrid::s_utmGrid = QStringLiteral("UTM");
const QString DisplayGrid::s_usngGrid = QStringLiteral("USNG");
const QString DisplayGrid::s_latlonGrid = QStringLiteral("LatLon");
const QString DisplayGrid::s_mgrsGrid = QStringLiteral("MGRS");
const QString DisplayGrid::s_ddFormat = QStringLiteral("Decimal degrees");
const QString DisplayGrid::s_dmsFormat = QStringLiteral( "Degrees minutes seconds");
const QString DisplayGrid::s_geographicPosition = QStringLiteral("Geographic");
const QString DisplayGrid::s_bottomLeftPosition = QStringLiteral("Bottom left");
const QString DisplayGrid::s_bottomRightPosition = QStringLiteral("Bottom right");
const QString DisplayGrid::s_topLeftPosition = QStringLiteral("Top left");
const QString DisplayGrid::s_topRightPosition = QStringLiteral("Top right");
const QString DisplayGrid::s_centerPosition = QStringLiteral("Center");
const QString DisplayGrid::s_allSidesPosition = QStringLiteral("All sides");

DisplayGrid::DisplayGrid(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

DisplayGrid::~DisplayGrid() = default;

void DisplayGrid::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<DisplayGrid>("Esri.Samples", 1, 0, "DisplayGridSample");
}

void DisplayGrid::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  // Create a map using the imagery basemap
  m_map = new Map(BasemapStyle::ArcGISImageryStandard, this);
  double targetScale = 6450785;
  m_map->setInitialViewpoint(Viewpoint(Point(-10336141.70018318, 5418213.05332071, SpatialReference::webMercator()), targetScale));

  // Set map to map view
  m_mapView->setMap(m_map);
}

// change the grid type
void DisplayGrid::changeGrid(const QString& gridType)
{
  if (gridType == latlonGrid())
  {
    //! [DisplayGrid Set_LatLon_Grid_Cpp]
    // create a grid for showing Latitude and Longitude (Meridians and Parallels)
    LatitudeLongitudeGrid* latlonGrid = new LatitudeLongitudeGrid(this);
    m_mapView->setGrid(latlonGrid);
    //! [DisplayGrid Set_LatLon_Grid_Cpp]
  }
  else if (gridType == utmGrid())
  {
    UTMGrid* utmGrid = new UTMGrid(this);
    m_mapView->setGrid(utmGrid);
  }
  else if (gridType == usngGrid())
  {
    USNGGrid* utmGrid = new USNGGrid(this);
    m_mapView->setGrid(utmGrid);
  }
  else if (gridType == mgrsGrid())
  {
    MGRSGrid* utmGrid = new MGRSGrid(this);
    m_mapView->setGrid(utmGrid);
  }

  // apply any styling that has been set
  changeGridColor(currentGridColor());
  changeLabelColor(currentLabelColor());
  changeLabelFormat(currentLabelFormat());
  changeLabelPosition(currentLabelPosition());
  setGridLabelVisibility(gridVisibility());
  setGridVisibility(gridVisibility());
}

// change the grid color
void DisplayGrid::changeGridColor(const QString& color)
{
  if (m_mapView->grid())
  {
    // find the number of resolution levels in the grid
    int gridLevels = m_mapView->grid()->levelCount();

    //! [DisplayGrid Set_Grid_Lines_Cpp]
    for (int level = 0; level < gridLevels; level++)
    {
      const float width = 1 + level;
      SimpleLineSymbol* lineSym = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(color), width, this);
      m_mapView->grid()->setLineSymbol(level, lineSym);
    }
    //! [DisplayGrid Set_Grid_Lines_Cpp]
  }
}

// change the grid label color
void DisplayGrid::changeLabelColor(const QString& color)
{
  if (m_mapView->grid())
  {
    //! [DisplayGrid Grid_Levels_Cpp]
    // find the number of resolution levels in the grid
    int gridLevels = m_mapView->grid()->levelCount();
    //! [DisplayGrid Grid_Levels_Cpp]

    //! [DisplayGrid Set_Grid_Labels_Cpp]
    for (int level = 0; level < gridLevels; level++)
    {
      constexpr float size = 14.0f;
      TextSymbol* textSym = new TextSymbol("text", QColor(color), size, HorizontalAlignment::Left, VerticalAlignment::Bottom, this);
      textSym->setHaloColor(QColor("white"));
      textSym->setHaloWidth(2.0f + level);
      m_mapView->grid()->setTextSymbol(level, textSym);
    }
    //! [DisplayGrid Set_Grid_Labels_Cpp]
  }
}

// change the grid label format
void DisplayGrid::changeLabelFormat(const QString& format)
{
  if (m_mapView->grid())
  {
    if (m_mapView->grid()->gridType() == GridType::LatitudeLongitudeGrid)
    {
      if (format == ddFormat())
      {
        // format the labels to display in decimal degrees
        static_cast<LatitudeLongitudeGrid*>(m_mapView->grid())->setLabelFormat(LatitudeLongitudeGridLabelFormat::DecimalDegrees);
      }
      else if (format == dmsFormat())
      {
        //! [DisplayGrid Set_Label_Format_Cpp]
        // format the labels to display in degrees, minutes and seconds
        static_cast<LatitudeLongitudeGrid*>(m_mapView->grid())->setLabelFormat(LatitudeLongitudeGridLabelFormat::DegreesMinutesSeconds);
        //! [DisplayGrid Set_Label_Format_Cpp]
      }
    }
  }
}

// change the grid label placement
void DisplayGrid::changeLabelPosition(const QString& position)
{
  if (m_mapView->grid())
  {
    if (position == geographicPosition())
    {
      //! [DisplayGrid Set_Label_Placement_Geographic_Cpp]
      // update the label positioning to geographic
      m_mapView->grid()->setLabelPosition(GridLabelPosition::Geographic);
      //! [DisplayGrid Set_Label_Placement_Geographic_Cpp]
    }
    else if (position == bottomLeftPosition())
    {
      // update the label positioning to bottom left
      m_mapView->grid()->setLabelPosition(GridLabelPosition::BottomLeft);
    }
    else if (position == bottomRightPosition())
    {
      // update the label positioning to bottom right
      m_mapView->grid()->setLabelPosition(GridLabelPosition::BottomRight);
    }
    else if (position == topLeftPosition())
    {
      // update the label positioning to top left
      m_mapView->grid()->setLabelPosition(GridLabelPosition::TopLeft);
    }
    else if (position == topRightPosition())
    {
      //! [DisplayGrid Set_Label_Placement_Top_Right_Cpp]
      // update the label positioning to top right
      m_mapView->grid()->setLabelPosition(GridLabelPosition::TopRight);
      //! [DisplayGrid Set_Label_Placement_Top_Right_Cpp]
    }
    else if (position == centerPosition())
    {
      // update the label positioning to center
      m_mapView->grid()->setLabelPosition(GridLabelPosition::Center);
    }
    else if (position == allSidesPosition())
    {
      // update the label positioning to all sides
      m_mapView->grid()->setLabelPosition(GridLabelPosition::AllSides);
    }
  }
}

MapQuickView* DisplayGrid::mapQuickView() const
{
  return m_mapView;
}

void DisplayGrid::setMapQuickView(MapQuickView* mapView)
{
  m_mapView = mapView;
  emit mapQuickViewChanged();
}

QString DisplayGrid::currentGridColor() const
{
  return m_currentGridColor;
}

void DisplayGrid::setCurrentGridColor(const QString& gridColor)
{
  m_currentGridColor = gridColor;
  changeGridColor(m_currentGridColor);
}

QString DisplayGrid::currentLabelColor() const
{
  return m_currentLabelColor;
}

void DisplayGrid::setCurrentLabelColor(const QString& labelColor)
{
  m_currentLabelColor = labelColor;
  changeLabelColor(m_currentLabelColor);
}

QString DisplayGrid::currentLabelFormat() const
{
  return m_currentLabelFormat;
}

void DisplayGrid::setCurrentLabelFormat(const QString& labelFormat)
{
  m_currentLabelFormat = labelFormat;
  changeLabelFormat(m_currentLabelFormat);
}

QString DisplayGrid::currentLabelPosition() const
{
  return m_currentLabelPosition;
}

void DisplayGrid::setCurrentLabelPosition(const QString& labelPosition)
{
  m_currentLabelPosition = labelPosition;
  changeLabelPosition(m_currentLabelPosition);
}

bool DisplayGrid::gridVisibility()
{
  return m_gridVisibility;
}

void DisplayGrid::setGridVisibility(bool visible)
{
  m_gridVisibility = visible;

  //! [DisplayGrid Grid_Visible_Cpp]
  m_mapView->grid()->setVisible(m_gridVisibility);
  //! [DisplayGrid Grid_Visible_Cpp]
}

bool DisplayGrid::gridLabelVisibility()
{
  return m_gridLabelVisibility;
}

void DisplayGrid::setGridLabelVisibility(bool visible)
{
  m_gridLabelVisibility = visible;

  //! [DisplayGrid Label_Visible_Cpp]
  m_mapView->grid()->setLabelsVisible(m_gridLabelVisibility);
  //! [DisplayGrid Label_Visible_Cpp]
}

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