Display route layer

View on GitHubSample viewer app

Display a route layer and its directions using a feature collection.

screenshot

Use case

Routes can be stored as feature collection layers. These layers can store useful information such as directions, estimated trip time, and more.

You can create a route layer in ArcGIS Pro and store a route layer as a portal item, making it easy to access, share, or display.

How to use the sample

Pan and zoom to view the route displayed by the feature collection layer. Press the Directions button to view the list of directions.

How it works

  1. Create a PortalItem with the item ID.
  2. Create and load a FeatureCollection with the item.
  3. After loading, get the specified FeatureCollectionTable by name.
  4. Get all of the ArcGISFeatures using FeatureTable::queryFeaturesAsync.
  5. Get the direction text from the attributes of each feature.
  6. Create a FeatureCollectionLayer with the feature collection and set it to the map's operationalLayers.

Relevant API

  • FeatureCollection
  • FeatureCollectionLayer
  • FeatureCollectionTableListModel
  • FeatureQueryResult
  • Portal
  • PortalItem

Tags

directions, feature collection, feature collection layer, portal, portal item, route layer, routing

Sample Code

DisplayRouteLayer.cppDisplayRouteLayer.cppDisplayRouteLayer.hDisplayRouteLayer.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
// [WriteFile Name=DisplayRouteLayer, Category=Routing]
// [Legal]
// Copyright 2022 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 "DisplayRouteLayer.h"

// ArcGIS Maps SDK headers
#include "ArcGISFeature.h"
#include "AttributeListModel.h"
#include "Error.h"
#include "Feature.h"
#include "FeatureCollection.h"
#include "FeatureCollectionLayer.h"
#include "FeatureCollectionTable.h"
#include "FeatureCollectionTableListModel.h"
#include "FeatureIterator.h"
#include "FeatureQueryResult.h"
#include "FeatureTable.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "Portal.h"
#include "PortalItem.h"
#include "PortalTypes.h"
#include "QueryParameters.h"
#include "SpatialReference.h"

// Qt headers
#include <QFuture>
#include <QJsonValue>

using namespace Esri::ArcGISRuntime;
namespace
{
static const QString featureCollectionItemId("0e3c8e86b4544274b45ecb61c9f41336");
}

DisplayRouteLayer::DisplayRouteLayer(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISTopographic, this))
{
  Portal* portal = new Portal(this);
  m_portalItem = new PortalItem(portal, featureCollectionItemId, this);

  connect(m_portalItem, &PortalItem::doneLoading, this, [this](const Error& loadError)
  {
    if (!loadError.isEmpty())
    {
      qWarning() << loadError.message() << loadError.additionalMessage();
      return;
    }

    // if the portal item is a feature collection, add the feature collection to the map's operational layers
    if (m_portalItem->type() == PortalItemType::FeatureCollection)
    {
      m_featureCollection = new FeatureCollection(m_portalItem, this);
      m_featureCollectionLayer = new FeatureCollectionLayer(m_featureCollection, this);

      connect(m_featureCollectionLayer, &FeatureCollectionLayer::doneLoading, this, [this](const Error& e)
      {
        if (!e.isEmpty())
          return;

        getDirections();
        emit enableDirectionsButton();
      });

      m_map->operationalLayers()->append(m_featureCollectionLayer);
    }
    else
    {
      qWarning() << "Portal item with ID " << featureCollectionItemId << " is not a feature collection.";
    }
  });

  m_portalItem->load();
}

DisplayRouteLayer::~DisplayRouteLayer() = default;

void DisplayRouteLayer::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<DisplayRouteLayer>("Esri.Samples", 1, 0, "DisplayRouteLayerSample");
}

MapQuickView* DisplayRouteLayer::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void DisplayRouteLayer::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);
  const Point vpCenter(-122.8309, 45.2281, SpatialReference(4326));
  m_mapView->setViewpointCenterAsync(vpCenter, 800000);

  emit mapViewChanged();
}

void DisplayRouteLayer::getDirections()
{
  if (!m_featureCollectionLayer)
    return;

  FeatureCollectionTableListModel* tables = m_featureCollection->tables();
  for (FeatureTable* table : *tables)
  {
    if (table->loadStatus() == LoadStatus::Loaded)
    {
      if (table->tableName() == "DirectionPoints")
      {
        QueryParameters queryParams;
        queryParams.setWhereClause("1=1");
        table->queryFeaturesAsync(queryParams).then(this, [this](const FeatureQueryResult* featureQueryResult)
        {
          if (!featureQueryResult)
            return;
          // Clear the directions list before repopulating it
          m_featureDirection.clear();

          while (featureQueryResult && featureQueryResult->iterator().hasNext())
          {
            m_feature = static_cast<ArcGISFeature*>(featureQueryResult->iterator().next(this));
            m_featureDirection = m_featureDirection + "\n - " + m_feature->attributes()->attributeValue(QStringLiteral("DisplayText")).toString();
            emit directionsChanged();
          }
        });
      }
    }
  }
}

QString DisplayRouteLayer::directions() const
{
  return m_featureDirection;
}

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