You will learn: how to load a layer from an ArcGIS Online item and add it to a map.
Applications can access
In this tutorial, you will add layers to the map that have been pre-configured and stored as an item in ArcGIS Online.
You must have previously installed the ArcGIS Runtime SDK for Qt and set up the development environment for your operating system.
If you have already completed the Create a starter app tutorial, start Qt Creator and open your starter app project. Otherwise, download and unzip the starter app project solution, and then open it in Qt Creator.
2e4b3df6ba4b44969a3bc9827de746b3
.In the Projects window, double click on Headers > Create_a_starter_app.h, add the namespace declaration to include Portal
and PortalItem
.
namespace Esri
{
namespace ArcGISRuntime
{
class Map;
class MapQuickView;
// *** ADD ***
class Portal;
class PortalItem;
Add two new private member variables to the same header file.
private:
Esri::ArcGISRuntime::MapQuickView* mapView() const;
void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
Esri::ArcGISRuntime::Map* m_map = nullptr;
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
// *** ADD ***
Esri::ArcGISRuntime::Portal* m_portal = nullptr;
Esri::ArcGISRuntime::PortalItem* m_portalItem = nullptr;
Create an anonymous connection to
In Projects, double click on Sources > Create_a_starter_app.cpp and add three new header files.
#include "MapQuickView.h"
// *** ADD ***
#include "Portal.h"
#include "PortalItem.h"
#include "FeatureLayer.h"
Add the following code to the constructor in order to load the Portal
for ArcGIS Online.
Create_a_starter_app::Create_a_starter_app(QObject* parent /* = nullptr */):
QObject(parent),
m_map(new Map(Basemap::topographicVector(this), this))
{
const Point center(-118.71511, 34.09042, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 300000.0);
// Set the initial viewpoint
m_map->setInitialViewpoint(viewpoint);
// *** ADD ***
const QUrl portal_url("https://www.arcgis.com");
m_portal = new Portal(portal_url, this);
connect(m_portal, &Portal::doneLoading, this, [](Esri::ArcGISRuntime::Error loadError)
{
if (!loadError.isEmpty())
qDebug() << loadError.message();
}
);
Add the following code to the constructor to load a PortalItem
from the ArcGIS Online portal.
Create_a_starter_app::Create_a_starter_app(QObject* parent /* = nullptr */):
QObject(parent),
m_map(new Map(Basemap::topographicVector(this), this))
{
const Point center(-118.71511, 34.09042, SpatialReference::wgs84());
const Viewpoint viewpoint(center, 300000.0);
// Set the initial viewpoint
m_map->setInitialViewpoint(viewpoint);
const QUrl portal_url("https://www.arcgis.com");
m_portal = new Portal(portal_url, this);
connect(m_portal, &Portal::doneLoading, this, [](Esri::ArcGISRuntime::Error loadError)
{
if (!loadError.isEmpty())
qDebug() << loadError.message();
}
);
// *** ADD ***
const QString portal_id("2e4b3df6ba4b44969a3bc9827de746b3");
m_portalItem = new PortalItem(m_portal, portal_id, this);
connect(m_portalItem, &PortalItem::doneLoading, this, [this]()
{
qDebug() << "Item title:" << this->m_portalItem->title();
}
);
In the Create_a_starter_app::setMapView
method, add the following code to create a feature layer and append it to the map.
void Add_a_layer_from_an_item::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
m_mapView->setMap(m_map);
// *** ADD ***
FeatureLayer* layer = new FeatureLayer(m_portalItem, 0, this);
m_map->operationalLayers()->append(layer);
Run your code to view the map.
Your app should display a map with the trailheads centered on the Santa Monica Mountains. Compare your solution with our completed solution project.
There are multiple ways to load a
Navigate to the page in ArcGIS Online for your
https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0
Create a ServiceFeatureTable
with this URL (where this
references the parent class which inherits QObject
).
const QUrl url("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0");
ServiceFeatureTable* table = new ServiceFeatureTable(url, this);
Create the FeatureLayer
from the service feature table.
FeatureLayer* layerFromTable = new FeatureLayer(table, this);
Add the layer to the map.
m_map->operationalLayers()->append(layerFromTable);
Learn how to create portal items with your own data in the Create a new dataset tutorial.
Use the same code pattern in step 5 and 7 to load two more layers into the map. Go to the Trails Styled and the Parks and Open Space Styled layers to find the item IDs and add the layers to the map.
If you have not already done so, visit the Discover data tutorial and search for open data you could use to add another layer to your app.