Mobile map (search and route)

View inC++QMLView on GitHubSample viewer app

Display maps and use locators to enable search and routing offline using a Mobile Map Package.

screenshot

Use case

Mobile map packages make it easy to transmit and store the necessary components for an offline map experience including: transportation networks (for routing/navigation), locators (address search, forward and reverse geocoding), and maps.

A field worker might download a mobile map package to support their operations while working offline.

How to use the sample

A list of maps from a mobile map package will be displayed. If the map contains transportation networks, the list item will have a navigation icon. Click on a map in the list to open it. If a locator task is available, click on the map to place a point. Click it again to reverse geocode the location's address. If transportation networks are available, click the route icon in the top right corner a route will be calculated between geocode locations.

How it works

  1. Create a MobileMapPackage passing in the path to the constructor.
  2. Get a list model of maps inside the package using the maps property.
  3. If the package has a locator, access it using the LocatorTask property.
  4. To see if a map contains transportation networks, check each map's TransportationNetworks property.

Relevant API

  • GeocodeResult
  • MobileMapPackage
  • ReverseGeocodeParameters
  • Route
  • RouteParameters
  • RouteResult
  • RouteTask
  • TransportationNetworkDataset

Offline data

Read more about how to set up the sample's offline data here.

Link Local Location
Yellowstone mmpk File <userhome>/ArcGIS/Runtime/Data/mmpk/Yellowstone.mmpk
SanFrancisco mmpk File <userhome>/ArcGIS/Runtime/Data/mmpk/SanFrancisco.mmpk

Tags

disconnected, field mobility, geocode, network, network analysis, offline, routing, search, transportation

Sample Code

MobileMap_SearchAndRoute.cppMobileMap_SearchAndRoute.cppMobileMap_SearchAndRoute.hMobileMap_SearchAndRoute.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// [WriteFile Name=MobileMap_SearchAndRoute, Category=Maps]
// [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 "MobileMap_SearchAndRoute.h"

#include "Map.h"
#include "Stop.h"
#include "Graphic.h"
#include "RouteTask.h"
#include "TextSymbol.h"
#include "LocatorTask.h"
#include "RouteResult.h"
#include "MapQuickView.h"
#include "GeocodeResult.h"
#include "GraphicsOverlay.h"
#include "RouteParameters.h"
#include "MobileMapPackage.h"
#include "PictureMarkerSymbol.h"
#include "ReverseGeocodeParameters.h"
#include "Error.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "CalloutData.h"
#include "GraphicsOverlayListModel.h"
#include "IdentifyGraphicsOverlayResult.h"
#include "SymbolTypes.h"
#include "AttributeListModel.h"
#include "GraphicListModel.h"
#include "Route.h"
#include "SimpleRenderer.h"
#include "SimpleLineSymbol.h"
#include "Item.h"
#include "Polyline.h"

#include <QUuid>
#include <QFileInfoList>
#include <QFile>
#include <QFuture>
#include <QtCore/qglobal.h>
#include <QDir>
#include <QStandardPaths>

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
QString defaultDataPath()
{
  QString dataPath;

#ifdef Q_OS_IOS
  dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
  dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
#endif

  return dataPath;
}
} // namespace

MobileMap_SearchAndRoute::MobileMap_SearchAndRoute(QQuickItem* parent):
  QQuickItem(parent),
  m_selectedMmpkIndex(0),
  m_canRoute(false),
  m_canClear(false),
  m_isGeocodeInProgress(false),
  m_dataPath(defaultDataPath() + "/ArcGIS/Runtime/Data/mmpk"),
  m_fileInfoList(QDir(m_dataPath).entryInfoList())
{
}

MobileMap_SearchAndRoute::~MobileMap_SearchAndRoute() = default;

void MobileMap_SearchAndRoute::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<MobileMap_SearchAndRoute>("Esri.Samples", 1, 0, "MobileMap_SearchAndRouteSample");
  qmlRegisterUncreatableType<CalloutData>("Esri.Samples", 1, 0, "CalloutData", "CalloutData is an uncreatable type");
}

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

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  // initialize Callout
  m_mapView->calloutData()->setTitle("Address");

  // set reverse geocoding parameters
  m_reverseGeocodeParameters.setMaxResults(1);

  // identify and create MobileMapPackages using mmpk files in datapath
  createMobileMapPackages(0);

  // create graphics overlays to visually display geocoding and routing results
  m_stopsGraphicsOverlay = new GraphicsOverlay(this);
  m_routeGraphicsOverlay = new GraphicsOverlay(this);
  m_routeGraphicsOverlay->setRenderer(new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("#2196F3"), 4, this), this));

  m_mapView->graphicsOverlays()->append(m_routeGraphicsOverlay);
  m_mapView->graphicsOverlays()->append(m_stopsGraphicsOverlay);

  // create a pin symbol
  m_bluePinSymbol = new PictureMarkerSymbol(QUrl("qrc:/Samples/Maps/MobileMap_SearchAndRoute/bluePinSymbol.png"), this);
  m_bluePinSymbol->setHeight(36);
  m_bluePinSymbol->setWidth(36);
  m_bluePinSymbol->setOffsetY(m_bluePinSymbol->height() / 2);

  connectSignals();
}

void MobileMap_SearchAndRoute::createMobileMapPackages(int index)
{
  if (index < m_fileInfoList.length())
  {
    // check if file is a .mmpk file
    if (m_fileInfoList[index].completeSuffix() == "mmpk")
    {
      // create a new MobileMapPackage
      MobileMapPackage* mobileMapPackage = new MobileMapPackage(m_fileInfoList[index].absoluteFilePath(), this);

      // once MMPK is finished loading, add it and its information to lists
      connect(mobileMapPackage, &MobileMapPackage::doneLoading, this, [mobileMapPackage, this](const Error& error)
      {
        if (error.isEmpty())
        {
          // QList of MobileMapPackages
          m_mobileMapPackages.append(mobileMapPackage);

          // QStringList of MobileMapPackage names. Used as a ListModel in QML
          m_mobileMapPackageList << mobileMapPackage->item()->title();
          emit mmpkListChanged();
        }
      });

      // load the new MMPK
      mobileMapPackage->load();
    }

    createMobileMapPackages(++index);
  }
  else
    return;
}

void MobileMap_SearchAndRoute::connectSignals()
{
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& mouseEvent)
  {
    if (!m_currentLocatorTask)
      return;
\
    m_clickedPoint = Point(m_mapView->screenToLocation(mouseEvent.position().x(), mouseEvent.position().y()));

    // determine if user clicked on a graphic
    m_mapView->identifyGraphicsOverlayAsync(m_stopsGraphicsOverlay, mouseEvent.position(), 5, false, 2).then(this,
    [this](const IdentifyGraphicsOverlayResult* identifyResult)
    {
      if (!identifyResult)
        return;

      // get graphics list
      QList<Graphic*> graphics = identifyResult->graphics();
      if (!graphics.isEmpty())
      {
        // use the blue pin graphic instead of text graphic to as calloutData's geoElement
        Graphic* graphic = graphics[0];
        if (graphic->symbol()->symbolType() != SymbolType::PictureMarkerSymbol && graphics.count() > 1)
          graphic = graphics[1];

        m_mapView->calloutData()->setGeoElement(graphic);
        m_mapView->calloutData()->setDetail(graphic->attributes()->attributeValue("AddressLabel").toString());
        m_mapView->calloutData()->setVisible(true);
      }
      // if clicked a point with no graphic on it, reverse geocode
      else
      {
        m_currentLocatorTask->reverseGeocodeWithParametersAsync(m_clickedPoint, m_reverseGeocodeParameters).then(this,
        [this](const QList<GeocodeResult>& geocodeResults)
        {
          // make busy indicator invisible
          m_isGeocodeInProgress = false;
          emit isGeocodeInProgressChanged();

          if (geocodeResults.isEmpty())
            return;

          // create parent for the graphic
          if (!m_stopGraphicParent)
            m_stopGraphicParent = new QObject(this);

          // create a blue pin graphic to display location
          Graphic* bluePinGraphic = new Graphic(geocodeResults[0].displayLocation(), m_bluePinSymbol, m_stopGraphicParent);
          bluePinGraphic->attributes()->insertAttribute("AddressLabel", geocodeResults[0].label());

          m_stopsGraphicsOverlay->graphics()->append(bluePinGraphic);

          // make clear graphics overlay button visible
          m_canClear = true;
          emit canClearChanged();

          // if routing is not enabled in map, return
          if (!m_currentRouteTask)
            return;

          //Set up the Stops

          // create a stop based on added graphic
          m_stops << Stop(geometry_cast<Point>(bluePinGraphic->geometry()));

          // create a Text Symbol to display stop number
          TextSymbol* textSymbol = new TextSymbol(m_stopGraphicParent);
          textSymbol->setText(QString::number(m_stops.count()));
          textSymbol->setColor(QColor("white"));
          textSymbol->setSize(18);
          textSymbol->setOffsetY(m_bluePinSymbol->height() / 2);

          // create a Graphic using the textSymbol
          Graphic* stopNumberGraphic = new Graphic(bluePinGraphic->geometry(), textSymbol, m_stopGraphicParent);
          stopNumberGraphic->setZIndex(bluePinGraphic->zIndex() + 1);
          m_stopsGraphicsOverlay->graphics()->append(stopNumberGraphic);

          if (m_stops.count() > 1)
          {
            m_canRoute = true;
            emit canRouteChanged();
          }
        });
        m_isGeocodeInProgress = true;
        emit isGeocodeInProgressChanged();
      }
    });
  });
}

void MobileMap_SearchAndRoute::resetMapView()
{
  // dismiss mapView controls
  m_canClear = false;
  emit canClearChanged();
  m_canRoute = false;
  emit canRouteChanged();

  // dimiss callout
  m_mapView->calloutData()->setVisible(false);

  // clear the graphics overlays and stops
  m_stopsGraphicsOverlay->graphics()->clear();
  if (m_stopGraphicParent)
  {
    delete m_stopGraphicParent;
    m_stopGraphicParent = nullptr;
  }
  m_routeGraphicsOverlay->graphics()->clear();
  if (m_routeGraphicParent)
  {
    delete m_routeGraphicParent;
    m_routeGraphicParent = nullptr;
  }
  m_stops.clear();
}

void MobileMap_SearchAndRoute::createMapList(int index)
{
  m_mapList.clear();
  m_selectedMmpkIndex = index;

  int counter = 1;

  const auto maps = m_mobileMapPackages[index]->maps();
  for (const Map* map : maps)
  {
    QVariantMap mapList;
    mapList["name"] = map->item()->title() + " " + QString::number(counter);
    mapList["geocoding"] = m_mobileMapPackages[index]->locatorTask() != nullptr;
    mapList["routing"] = map->transportationNetworks().count() > 0;

    m_mapList << mapList;
    ++counter;
  }

  emit mapListChanged();
}

void MobileMap_SearchAndRoute::selectMap(int index)
{
  resetMapView();

  // set the locatorTask
  //! [MobileMap_SearchAndRoute create LocatorTask]
  m_currentLocatorTask = m_mobileMapPackages[m_selectedMmpkIndex]->locatorTask();
  //! [MobileMap_SearchAndRoute create LocatorTask]

  // set the MapView
  m_mapView->setMap(m_mobileMapPackages[m_selectedMmpkIndex]->maps().at(index));

  // create a RouteTask with selected map's transportation network if available
  if (m_mobileMapPackages[m_selectedMmpkIndex]->maps().at(index)->transportationNetworks().count() > 0)
  {
    m_currentRouteTask = new RouteTask(m_mobileMapPackages[m_selectedMmpkIndex]->maps().at(index)->transportationNetworks().at(0), this);
    m_currentRouteTask->load();

    // create default parameters after the RouteTask is loaded
    connect(m_currentRouteTask, &RouteTask::loadStatusChanged, this, [this](LoadStatus loadStatus)
    {
      if (loadStatus == LoadStatus::Loaded)
        m_currentRouteTask->createDefaultParametersAsync().then(this, [this](const RouteParameters& routeParameters)
        {
          m_currentRouteParameters = routeParameters;
        });
    });
  }
  else
    m_currentRouteTask = nullptr;
}

void MobileMap_SearchAndRoute::solveRoute()
{
  // clear previously displayed routes
  m_routeGraphicsOverlay->graphics()->clear();
  if (m_routeGraphicParent)
  {
    delete m_routeGraphicParent;
    m_routeGraphicParent = nullptr;
  }

  // set stops and solve route
  m_currentRouteParameters.setStops(m_stops);
  // create a graphic using the RouteResult
  m_currentRouteTask->solveRouteAsync(m_currentRouteParameters).then(this,
  [this](const RouteResult& routeResult)
  {
    if (!m_routeGraphicParent)
      m_routeGraphicParent = new QObject(this);

    if (!routeResult.isEmpty())
    {
      const auto routes = routeResult.routes();
      Graphic* routeGraphic = new Graphic(routes[0].routeGeometry(), m_routeGraphicParent);
      m_routeGraphicsOverlay->graphics()->append(routeGraphic);
    }
  });
}

QStringList MobileMap_SearchAndRoute::mmpkList() const
{
  return m_mobileMapPackageList;
}

QVariantList MobileMap_SearchAndRoute::mapList() const
{
  return m_mapList;
}

bool MobileMap_SearchAndRoute::isGeocodeInProgress() const
{
  return m_isGeocodeInProgress;
}

bool MobileMap_SearchAndRoute::canRoute() const
{
  return m_canRoute;
}

bool MobileMap_SearchAndRoute::canClear() const
{
  return m_canClear;
}

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