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
390
391
392
393
394
395
396
397
398
399
400
// [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 "TaskWatcher.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 <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)
{
m_clickedPoint = Point(m_mapView->screenToLocation(mouseEvent.position().x(), mouseEvent.position().y()));
// determine if user clicked on a graphic
m_mapView->identifyGraphicsOverlay(m_stopsGraphicsOverlay, mouseEvent.position().x(), mouseEvent.position().y(), 5, false, 2);
}
});
connect(m_mapView, &MapQuickView::identifyGraphicsOverlayCompleted, this, [this](const QUuid&, IdentifyGraphicsOverlayResult* identifyResult)
{
if (!identifyResult)
return;
// get graphics list
QList<Graphic*> graphics = identifyResult->graphics();
if (graphics.count() > 0)
{
// 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->reverseGeocodeWithParameters(m_clickedPoint, m_reverseGeocodeParameters);
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));
if (m_currentLocatorTask)
{
// prevent connecting same signal to multiple slots
m_currentLocatorTask->disconnect();
connect(m_currentLocatorTask, &LocatorTask::geocodeCompleted, this, [this](const QUuid&, const QList<GeocodeResult>& geocodeResults)
{
// make busy indicator invisible
m_isGeocodeInProgress = false;
emit isGeocodeInProgressChanged();
if (geocodeResults.count() > 0)
{
// 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 enabled in map, set up the Stops
if (m_currentRouteTask)
{
// 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();
}
}
}
});
}
// 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->createDefaultParameters();
});
// store the created route parameters
connect(m_currentRouteTask, &RouteTask::createDefaultParametersCompleted, this, [this](const QUuid&, const RouteParameters& routeParameters)
{
m_currentRouteParameters = routeParameters;
});
// create a graphic using the routeResult
connect(m_currentRouteTask, &RouteTask::solveRouteCompleted, this, [this](const QUuid&, 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);
}
});
}
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);
m_currentRouteTask->solveRoute(m_currentRouteParameters);
}
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;
}