Format coordinates

View on GitHubSample viewer app

Format coordinates in a variety of common notations.

screenshot

Use case

The coordinate formatter can format a map location in WGS84 in a number of common coordinate notations. Parsing one of these formats to a location is also supported. Formats include decimal degrees; degrees, minutes, seconds; Universal Transverse Mercator (UTM), and United States National Grid (USNG).

How to use the sample

Click on the map to see a callout with the clicked location's coordinate formatted in 4 different ways. You can also put a coordinate string in any of these formats in the text field. Hit Enter and the coordinate string will be parsed to a map location which the callout will move to.

How it works

  1. Get or create a map Point with a spatial reference.
  2. Use one of the static "to" methods on CoordinateFormatter such as CoordinateFormatter::toLatitudeLongitude(point, LatitudeLongitudeFormat::DecimalDegrees, 4) to get the formatted string.
  3. To go from a formatted string to a Point, use one of the "from" static methods like CoordinateFormatter::fromUtm(coordinateString, map.spatialReference(), UtmConversionMode::LatitudeBandIndicators).

Relevant API

  • CoordinateFormatter
  • LatitudeLongitudeFormat
  • UtmConversionMode

Tags

convert, coordinate, decimal degrees, degree minutes seconds, format, latitude, longitude, USNG, UTM

Sample Code

FormatCoordinates.cppFormatCoordinates.cppFormatCoordinates.hFormatCoordinates.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
// [WriteFile Name=FormatCoordinates, Category=Geometry]
// [Legal]
// Copyright 2017 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

// sample headers
#include "FormatCoordinates.h"

// ArcGIS Maps SDK headers
#include "CoordinateFormatter.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "SimpleMarkerSymbol.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"

using namespace Esri::ArcGISRuntime;

namespace
{
  // Initial point marker 'X' symbol appears.
  const Point startPoint(-117.195723, 34.056195, SpatialReference::wgs84());
}

FormatCoordinates::FormatCoordinates(QObject* parent) :
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISImageryStandard, this)),
  m_graphicsOverlay(new GraphicsOverlay(this))
{
  // create a graphic
  SimpleMarkerSymbol* symbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::X, QColor(Qt::red), 15.0, this);

  Graphic* graphic = new Graphic(startPoint, symbol, this);

  // and add insert the graphic
  m_graphicsOverlay->graphics()->append(graphic);
}

FormatCoordinates::~FormatCoordinates() = default;

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

// handle case where the user changed one of the text fields
void FormatCoordinates::handleTextUpdate(QString textType, QString text)
{
  Point point = createPointFromText(textType, text);
  handleLocationUpdate(std::move(point));
}

// handle case where user clicked on the map
void FormatCoordinates::handleLocationUpdate(Point point)
{
  if (!point.isEmpty())
  {
    m_mapView->graphicsOverlays()->at(0)->graphics()->at(0)->setGeometry(point);
    setTextFromPoint(point);
  }
}

Point FormatCoordinates::createPointFromText(const QString& textType, const QString& text) const
{
  //! [FormatCoordinates CoordinateFormatter various text to point]
  if (strDecimalDegrees() == textType
      || strDegreesMinutesSeconds() == textType) {
    return CoordinateFormatter::fromLatitudeLongitude(text, m_map->spatialReference());
  }
  if (strUsng() == textType) {
    return CoordinateFormatter::fromUsng(text, m_map->spatialReference());
  }
  if (strUtm() == textType) {
    return CoordinateFormatter::fromUtm(text, m_map->spatialReference(), UtmConversionMode::LatitudeBandIndicators);
  }
  return Point();
  //! [FormatCoordinates CoordinateFormatter various text to point]
}

void FormatCoordinates::setTextFromPoint(Point point)
{
  // last parm = decimal places
  m_coordinatesInDD = CoordinateFormatter::toLatitudeLongitude(point, LatitudeLongitudeFormat::DecimalDegrees, 6);

  // last parm = decimal places
  m_coordinatesInDMS = CoordinateFormatter::toLatitudeLongitude(point, LatitudeLongitudeFormat::DegreesMinutesSeconds, 1);

  //! [FormatCoordinates CoordinateFormatter point to USNG]
  int decimalPlaces = 5;
  bool addSpaces = true;
  m_coordinatesInUsng = CoordinateFormatter::toUsng(point, decimalPlaces, addSpaces);
  //! [FormatCoordinates CoordinateFormatter point to USNG]

  // last parm = add spaces
  m_coordinatesInUtm = CoordinateFormatter::toUtm(point, UtmConversionMode::LatitudeBandIndicators, true);

  emit coordinatesChanged();
}

QString FormatCoordinates::coordinatesInDD() const
{
  return m_coordinatesInDD;
}

QString FormatCoordinates::coordinatesInDMS() const
{
  return m_coordinatesInDMS;
}

QString FormatCoordinates::coordinatesInUsng() const
{
  return m_coordinatesInUsng;
}

QString FormatCoordinates::coordinatesInUtm() const
{
  return m_coordinatesInUtm;
}

QString FormatCoordinates::strDecimalDegrees() const
{
  return tr("Degrees");
}

QString FormatCoordinates::strDegreesMinutesSeconds() const
{
  return tr("DMS");
}

QString FormatCoordinates::strUsng() const
{
  return tr("Usng");
}

QString FormatCoordinates::strUtm() const
{
  return tr("Utm");
}

void FormatCoordinates::setMapView(MapQuickView* mapView)
{
  if (m_mapView)
  {
    m_mapView->setMap(nullptr); // Remove map from old mapView.
    m_mapView->graphicsOverlays()->clear(); // Remove Graphics overlays from old mapView.
  }

  m_mapView = mapView;

  if (!m_mapView)
  {
    return;
  }

  // set map and graphics overlay on the map view
  m_mapView->setMap(m_map);
  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

  // connect to the mouse clicked signal on the MapQuickView
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& mouseEvent)
  {
    // get the point from the mouse point
    Point mapPoint = m_mapView->screenToLocation(mouseEvent.position().x(), mouseEvent.position().y());

    // using the point, refresh the graphic and the text
    handleLocationUpdate(std::move(mapPoint));
  });

  handleLocationUpdate(startPoint);
}

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