Learn how to find an address or place with a search bar and the geocoding service

Geocoding is the process of converting address or place
In this tutorial, you use a search bar in the user interface to access the Geocoding service and search for addresses and places.
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
-
The ArcGIS Maps SDK for Qt, version 300.0.0 or later is installed.
-
The Qt 6.8.2 software development framework or later is installed.
Set up authentication
To access the secure ArcGIS location services
You can implement API key authentication or user authentication in this tutorial. Compare the differences below:
API key authentication
- Users are not required to sign in.
- Requires creating an API key credential
API key credentials are an item that contains the parameters used to create and manage long-lived access tokens for API key authentication. They are a type of developer credential. with the correct privileges. - API keys
An API key is a long-lived access token created using API key credentials. They are valid for up to one year and are typically embedded directly into client applications. are long-lived access tokens. - Service usage is billed to the API key owner/developer.
- Simplest authentication method to implement.
- Recommended approach for new ArcGIS developers.
Learn more in API key authentication.
User authentication
- Users are required to sign in with an ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. . - User accounts must have privilege
Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. to access the ArcGIS servicesA service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. used in application. - Requires creating OAuth credentials
OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a .client_id,client_secret, and redirect URIs. They are a type of developer credential. - Application uses a redirect URL and client ID.
- Service usage is billed to the organization of the user signed into the application.
Learn more in User authentication.
To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User authentication.
Create a new API key access token
-
Complete the Create an API key tutorial and create an API key with the following privilege(s)
Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. :- Privileges
- Location services > Basemaps
- Location services > Geocoding
- Privileges
-
Copy and paste the API key access token into a safe location. It will be used in a later step.
Create new OAuth credentials to access the secure resources used in this tutorial.
-
Complete the Create OAuth credentials for user authentication tutorial to obtain a Client ID and Redirect URL.
A
Client IDuniquely identifies your app on the authenticating server. If the server cannot find an app with the provided Client ID, it will not proceed with authentication.The
Redirect URL(also referred to as a callback url) is used to identify a response from the authenticating server when the system returns control back to your app after an OAuth login. Since it does not necessarily represent a valid endpoint that a user could navigate to, the redirect URL can use a custom scheme, such asmy-app://auth. It is important to make sure the redirect URL used in your app’s code matches a redirect URL configured on the authenticating server. -
Copy and paste the Client ID and Redirect URL into a safe location. They will be used in a later step.
All users that access this application need account privileges
Develop or Download
You have two options for completing this tutorial:
Option 1: Develop the code
To start the tutorial, complete the Display a map tutorial. This creates a map to display the Santa Monica Mountains in California using the topographic basemap from the ArcGIS Basemap Styles service
Open a Qt Creator project
- Open the project you created by completing the Display a map tutorial.
- Continue with the following instructions to add a search bar to the user interface and search for an address or place using the ArcGIS Geocoding service
A geocoding service is a service that can search for addresses, place addresses, businesses, reverse geocode coordinates to addresses, provide suggestions for places, and perform bulk geocoding. It is hosted by Esri as the ArcGIS Geocoding service and can also be hosted in ArcGIS Enterprise. .
Declare classes, member variables, functions, and signals
-
Double click on Headers > Display_a_map.h to open the file. Declare the classes shown.
Display_a_map.hnamespace Esri::ArcGISRuntime {class Map;class MapQuickView;class GraphicsOverlay;class Graphic;class LocatorTask;class GeocodeResult;class SuggestResult;class TextSymbol; -
Staying in the same file, add the following two
#includestatements as shown.Display_a_map.h#include <QObject>Q_MOC_INCLUDE("MapQuickView.h")#include <QAbstractListModel>#include "GeocodeParameters.h" -
Continuing in the same file, add a
Q_PROPERTYfor the member variablesuggestions.Display_a_map.hclass Display_a_map : public QObject{Q_OBJECTQ_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged) -
Add the following public member functions with the
Q_INVOKABLEmacro to allow these to be invokable from the GUI.Display_a_map.hpublic:explicit Display_a_map(QObject* parent = nullptr);~Display_a_map() override;Q_INVOKABLE void geocode(const QString& query);Q_INVOKABLE void clearGraphics();Q_INVOKABLE void setSuggestions(const QString& text); -
Add two new signal declarations.
Display_a_map.hsignals:void mapViewChanged();void suggestionsChanged();void hideSuggestionView(); -
Declare the
setupLocatorTaskprivate method.Display_a_map.hprivate:Esri::ArcGISRuntime::MapQuickView* mapView() const;void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);void setupViewpoint();void setupLocatorTask(); -
Finally in the Display_a_map.h file, add the following six private member functions and variables. Then save the file.
Display_a_map.hprivate:Esri::ArcGISRuntime::MapQuickView* mapView() const;void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);void setupViewpoint();void setupLocatorTask();Esri::ArcGISRuntime::Map* m_map = nullptr;Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;void configureGraphic();QAbstractListModel* suggestions() const;Esri::ArcGISRuntime::GraphicsOverlay* m_graphicsOverlay = nullptr;Esri::ArcGISRuntime::LocatorTask* m_locatorTask = nullptr;Esri::ArcGISRuntime::Graphic* m_graphicResultLocation = nullptr;Esri::ArcGISRuntime::Graphic* m_graphicResultText = nullptr;Esri::ArcGISRuntime::TextSymbol* m_textSymbol = nullptr;QAbstractListModel* m_suggestListModel = nullptr;Esri::ArcGISRuntime::GeocodeParameters m_geocodeParams;
Include header files to access needed classes
-
In Projects, double click on Sources > Display_a_map.cpp to open the file and add the following
#includestatements.Display_a_map.cpp#include "Display_a_map.h"#include "Map.h"#include "MapTypes.h"#include "MapQuickView.h"#include "Point.h"#include "Viewpoint.h"#include "SpatialReference.h"#include <QFuture>#include "AttributeListModel.h"#include "Envelope.h"#include "GeocodeResult.h"#include "Graphic.h"#include "GraphicListModel.h"#include "GraphicsOverlay.h"#include "GraphicsOverlayListModel.h"#include "LocatorTask.h"#include "SimpleMarkerSymbol.h"#include "SimpleRenderer.h"#include "SymbolTypes.h"#include "SuggestParameters.h"#include "SuggestListModel.h"#include "TextSymbol.h" -
Continuing in the same file, include the following Qt classes.
Display_a_map.cpp#include "AttributeListModel.h"#include "Envelope.h"#include "GeocodeResult.h"#include "Graphic.h"#include "GraphicListModel.h"#include "GraphicsOverlay.h"#include "GraphicsOverlayListModel.h"#include "LocatorTask.h"#include "SimpleMarkerSymbol.h"#include "SimpleRenderer.h"#include "SymbolTypes.h"#include "SuggestParameters.h"#include "SuggestListModel.h"#include "TextSymbol.h"#include <QAbstractListModel>#include <QGeoPositionInfoSource>#include <QUrl>#include <QUuid>
Create a locator task with geocode parameters
Geocoding is implemented with a locator
-
Staying in the Display_a_map.cpp file, add the call to
setupLocatorTask. This method will be implemented next.Display_a_map.cppDisplay_a_map::Display_a_map(QObject* parent /* = nullptr */):QObject(parent),m_map(new Map(BasemapStyle::ArcGISTopographic, this)){setupLocatorTask(); -
Continuing in the same file, add code to begin implementing the
setupLocatorTaskmethod and initialize the auto-suggestion list. Within the method body, create a newLocatorTaskbased on the Geocoding service, and set it to them_locatorTaskmember variable. Additionally, create aSuggestParameters, instance and initialize it with three categories as shown. Then set max results to limit for the number of returned suggestion results to 5. Finally, configureGeocodeParameters, (m_geocodeParams). Add code to set the minimum match score and the attribute names for the results to be returned. Callsuggestionsonm_locatorTaskand assign tom_suggestListModel.Display_a_map.cppDisplay_a_map::~Display_a_map() = default;void Display_a_map::setupLocatorTask(){const QUrl geocode_service("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");m_locatorTask = new LocatorTask(geocode_service, this);SuggestParameters suggestParams;const QStringList categories{"Address", "POI", "Populated Place"};suggestParams.setCategories(categories);suggestParams.setMaxResults(5);m_locatorTask->suggestions()->setSuggestParameters(suggestParams);m_geocodeParams.setMinScore(75);m_geocodeParams.setResultAttributeNames(QStringList {"Place_addr", "Match_addr"});m_suggestListModel = m_locatorTask->suggestions();emit suggestionsChanged();}A locator task is used to convert an address to a point (geocode) or vice-versa (reverse geocode). An address includes any type of information that distinguishes a place. A locator
A locator is an ArcGIS dataset that stores address information and the rules for translating descriptions of places (such as street addresses or place names) into spatial data that can be displayed on a map. involves finding matching locations for a given address. Reverse-geocoding is the opposite and finds the closest address for a given location.
Create the auto-suggestion feature
-
Staying in the Display_a_map.cpp file, towards the bottom of the file, add a new
setSuggestions(const QString& text)method to implement the auto-suggestion feature. This generates a list of suggested addresses based on the user’s input in the GUI text field.Display_a_map.cppsetupViewpoint();emit mapViewChanged();}void Display_a_map::setSuggestions(const QString& text){if (!m_suggestListModel)return;SuggestListModel* suggestListModel = dynamic_cast<SuggestListModel*>(m_suggestListModel);if (!suggestListModel)return;suggestListModel->setSearchText(text);} -
Update the
setupViewpointmethod to respond to the user’s mouse selection of an address from the list of suggested addresses. Inject the following code immediately after the open curly braces but before the other code in the function.Display_a_map.cppvoid Display_a_map::setupViewpoint(){connect(m_mapView, &MapQuickView::mousePressed, this, [this](QMouseEvent& /* event */){emit hideSuggestionView();});const Point center(-118.80543, 34.02700, SpatialReference::wgs84());const Viewpoint viewpoint(center, 100000.0);m_mapView->setViewpointAsync(viewpoint);}
Add text and marker graphics to identify location on map
-
Continuing in the Display_a_map.cpp file, towards the bottom of the file, again update the
setupViewpointmethod to add the call toconfigureGraphic(), which will be implemented in the next step.Display_a_map.cppvoid Display_a_map::setupViewpoint(){connect(m_mapView, &MapQuickView::mousePressed, this, [this](QMouseEvent& /* event */){emit hideSuggestionView();});const Point center(-118.80543, 34.02700, SpatialReference::wgs84());const Viewpoint viewpoint(center, 100000.0);m_mapView->setViewpointAsync(viewpoint);configureGraphic();} -
Still working in the Display_a_map.cpp file, implement the
configureGraphicpublic method. Create aSimpleMarkerSymbol(blue square), initialize aGraphicand add that to aGraphicsOverlay. Then createTextSymbol, initialize aGraphicwith that, and add it to theGraphicsOverlay. Then add theGraphicsOverlaytoMapView.Display_a_map.cppif (!suggestListModel)return;suggestListModel->setSearchText(text);}void Display_a_map::configureGraphic(){if (m_graphicResultLocation)return;// create graphics overlay and add to map viewm_graphicsOverlay = new GraphicsOverlay(this);// set a renderer on the graphics overlaySimpleRenderer* simpleRenderer = new SimpleRenderer(this);// Create a graphic and symbol to display the result location.SimpleMarkerSymbol* simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor("blue"), 12.0, this);simpleRenderer->setSymbol(simpleMarkerSymbol);m_graphicResultLocation = new Graphic(this);m_graphicResultLocation->setSymbol(simpleMarkerSymbol);m_graphicsOverlay->graphics()->append(m_graphicResultLocation);// Create a graphic and symbol to display a label next to the result locationm_textSymbol = new TextSymbol("", QColor("red"), 18.0, HorizontalAlignment::Center, VerticalAlignment::Bottom, this);m_graphicResultText = new Graphic(this);m_graphicResultText->setSymbol(m_textSymbol);m_graphicsOverlay->graphics()->append(m_graphicResultText);m_mapView->graphicsOverlays()->append(m_graphicsOverlay);}
Add the Geocode method
An asynchronous geocode operation is required to find and return the location candidates for a given address and geocode parameters.
-
Staying in the Display_a_map.cpp file, add the
geocode(const QString& query)method to geocode an address when the user clicks on that address in the auto-suggestion list. This method will invoke the geocode with parameters async method on theLocatorTask. Within the body of theifstatement, the code checks for geocoding results and verifies that the graphic exists. If both conditions are met, the result location is set as the graphic’s geometry and the attributes provided by the result are copied to the graphic. The map view display is centered on the graphic before making it visible. In the final block of code, the text symbol is set, the text graphic geometry for the geocoding result display location is set, the attributes for the text graphic is set, and graphic is made visible.Display_a_map.cppm_graphicsOverlay->graphics()->append(m_graphicResultText);m_mapView->graphicsOverlays()->append(m_graphicsOverlay);}void Display_a_map::geocode(const QString& query){m_locatorTask->geocodeWithParametersAsync(query, m_geocodeParams).then(this,[this](const QList<GeocodeResult>& geocodeResults){if (!geocodeResults.isEmpty() && m_graphicResultLocation){// display geocode result label and positionconst GeocodeResult geocodeResult = geocodeResults.at(0);m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());constexpr double scale = 8000.0;m_mapView->setViewpointCenterAsync(geocodeResult.extent().center(), scale);m_graphicResultLocation->setVisible(true);m_textSymbol->setText(geocodeResult.label());m_graphicResultText->setGeometry(geocodeResult.displayLocation());m_graphicResultText->attributes()->setAttributesMap(geocodeResult.attributes());m_graphicResultLocation->setVisible(true);}});} -
Add the
suggestionsmethod to register the value ofm_suggestListModelas the value of the QML propertysuggestions.Display_a_map.cppm_graphicResultLocation->setVisible(true);m_textSymbol->setText(geocodeResult.label());m_graphicResultText->setGeometry(geocodeResult.displayLocation());m_graphicResultText->attributes()->setAttributesMap(geocodeResult.attributes());m_graphicResultLocation->setVisible(true);}});}QAbstractListModel* Display_a_map::suggestions() const{return m_suggestListModel;} -
Finally in the Display_a_map.cpp file, add the
clearGraphicsmethod. Then save the file.Display_a_map.cppQAbstractListModel* Display_a_map::suggestions() const{return m_suggestListModel;}void Display_a_map::clearGraphics(){m_graphicResultLocation->setGeometry(Point());m_graphicResultText->setGeometry(Point());}
Create the application GUI
The GUI will let you enter partial or full addresses into a field, see a list of matching addresses, click an address and zoom to that location, and clear the address field.
-
In Projects, click on Resources > qml\qml.qrc > /qml. Then open the file Display_a_mapForm.qml
-
Add the following import statement.
Display_a_mapForm.qmlimport QtQuickimport QtQuick.Controlsimport Esri.Display_a_mapimport QtQuick.Layouts -
Change the line of code for the
MapViewcomponent id from:modeltosearch. See the yellow changed line.Display_a_mapForm.qml// Create MapQuickView here, and create its Map etc. in C++ codeMapView {id: viewanchors.fill: parent// set focus to enable keyboard navigationfocus: true}// Declare the C++ instance that creates the map etc. and supply the viewDisplay_a_map {id: searchmapView: view} -
Add the following QML code to implement the app GUI.
Display_a_mapForm.qml// Declare the C++ instance that creates the map etc. and supply the viewDisplay_a_map {id: searchmapView: view}Rectangle {// add rectangle for gui codeid: searchRectwidth: parent.widthheight: childrenRect.heightproperty int cellHeight: 40;Column {anchors {fill: parentmargins: 10}Rectangle {color: "#f7f8fa"border {color: "#7B7C7D"}radius: 2width: parent.widthheight: childrenRect.heightGridLayout {width: parent.widthcolumns: 4TextField {id: textFieldLayout.margins: 5Layout.fillWidth: truefont.pixelSize: 14placeholderText: "Type in an address"selectByMouse: trueonTextChanged: {if (text.length > 0 && suggestView)suggestView.visible = true;search.setSuggestions(text);}onAccepted: {suggestView.visible = false;search.geocode(textField.text);Qt.inputMethod.hide();}}Rectangle {Layout.margins: 5width: heightheight: textField.heightcolor: "#f7f8fa"visible: textField.length === 0enabled: visibleButton {anchors.fill: parenttext: "X"font.pixelSize: 16MouseArea {anchors.fill: parentonClicked: {textField.focus = true;suggestView.visible = !suggestView.visible;}}}}Rectangle {Layout.margins: 5width: heightcolor: "transparent"height: textField.heightvisible: textField.length !== 0enabled: visibleButton {anchors.fill: parenttext: "X"font.pixelSize: 16MouseArea {anchors.fill: parentonClicked: {textField.text = "";search.clearGraphics();}}}}}}//Tutorial add ListView to display suggested location results and bind it to the locator task list model.ListView {id: suggestViewheight: 20 * searchRect.cellHeightwidth: textField.widthmodel: search.suggestionsvisible: falseclip: truedelegate: Component {Rectangle {id: rectwidth: textField.widthheight: searchRect.cellHeightcolor: "#f7f8fa"Text {anchors {verticalCenter: parent.verticalCenterleftMargin: 5rightMargin: 5}font {weight: Font.BlackpixelSize: 16}width: textField.widthtext: labelelide: Text.ElideRightcolor: "black"}MouseArea {anchors.fill: parentonClicked: {textField.text = label;suggestView.visible = false;search.geocode(label);Qt.inputMethod.hide();}}}}}}}
Set developer credentials
For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.
Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.
Set the API Key
-
In the project Sources folder of Qt Creator, open the main.cpp file.
-
Modify the code to set the
accessTokenusing your API key access token (highlighted in yellow).main.cpp// The following methods grant an access token:// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account.// To generate a token, a user logs in to the app with an ArcGIS account that is part of an// organization in ArcGIS Online or ArcGIS Enterprise.// 2. API key authentication: Get a long-lived access token that gives your application access to// ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key.// Copy the API Key access token.const QString accessToken = QString("");if (accessToken.isEmpty()){qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" <<"you to authenticate with an ArcGIS account or set the API Key property.";}else{ArcGISRuntimeEnvironment::setApiKey(accessToken);} -
Save the main.cpp file.
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Set path to the Qt Toolkit in the project
In the project Sources folder of Qt Creator, open the Display_a_map.pro file, locate the following lines, and update the PATH_TO_TOOLKIT variable with the path of the toolkitcpp.pri file (highlighted in yellow). Otherwise, the OAuth dialog will not appear to enter your user credentials. Then save the file.
ARCGIS_RUNTIME_VERSION = 300.0.0include($$PWD/arcgisruntime.pri)
# TODO: You need to replace the <path_to_toolkit_repo> with a valid location where the Qt Toolkit# resides on your system, example: C:/arcgis-maps-sdk-toolkit-qt/uitools/toolkitcpp/toolkitcpp.pri# This block determines whether you've cloned your toolkitPATH_TO_TOOLKIT = "<path_to_toolkit_repo>/uitools/toolkitcpp/toolkitcpp.pri"
exists($${PATH_TO_TOOLKIT}) { message("Toolkit found") DEFINES += TOOLKIT_FOUND
# include the toolkitcpp.pri, which contains all the toolkit resources include($${PATH_TO_TOOLKIT})
qtHaveModule(webenginequick) { QT += webenginequick }} else { message("Toolkit not found in provided path. Either set PATH_TO_TOOLKIT or use an API Key")}Set developer credentials in the solution
In the project Sources folder of Qt Creator, open the Display_a_map.cpp file.
Set your values for the REDIRECT_URL and the CLIENT_ID strings (highlighted in yellow). Then save the file.
{
#ifdef TOOLKIT_FOUND
// Define the Redirect URL string obtained when creating the OAuth credentials. // TODO: You need to replace the "REDIRECT_URL" with your own valid string, // ex: "urn:ietf:wg:oauth:2.0:oob" const auto qStringRedirectUrl = QString{"REDIRECT_URL"};
// Define the URL of the portal to authenticate with. const QUrl qUrlPortal = QUrl{"https://www.arcgis.com/"};
// Define a unique identifier associated with an application registered with the // portal that assists with client/server OAuth authentication. // TODO: You need to replace the "CLIENT_ID" with your own valid string. const QString qStringClientId = QString{"CLIENT_ID"};
// Create a new OAuth user configuration using: the Url to mapping web service, the Client ID // string, and the Redirect Url string. auto* oAuthUserConfiguration = new OAuthUserConfiguration(qUrlPortal, qStringClientId, qStringRedirectUrl, this);
// Call the Toolkit's OAuthUserConfigurationManager static `addConfiguration` // method to use the OAuth dialog. This will tell the Authenticator to use OAuth for the provided configuration. Toolkit::OAuthUserConfigurationManager::addConfiguration(oAuthUserConfiguration);#endif // TOOLKIT_FOUNDPress Ctrl + R to run the app.
You should see a map
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the
Download solutionlink underSolutionand unzip the file to a location on your machine. -
Open the .pro project file in Qt Creator.
Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the Set up authentication section.
For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.
Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.
Set developer credentials in the solution
Set the API Key
-
In the project Sources folder of Qt Creator, open the main.cpp file.
-
Modify the code to set the
accessTokenusing your API key access token (highlighted in yellow).main.cpp// The following methods grant an access token:// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account.// To generate a token, a user logs in to the app with an ArcGIS account that is part of an// organization in ArcGIS Online or ArcGIS Enterprise.// 2. API key authentication: Get a long-lived access token that gives your application access to// ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key.// Copy the API Key access token.const QString accessToken = QString("");if (accessToken.isEmpty()){qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" <<"you to authenticate with an ArcGIS account or set the API Key property.";}else{ArcGISRuntimeEnvironment::setApiKey(accessToken);} -
Save main.cpp file.
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Set path to the Qt Toolkit in the project
In the project Sources folder of Qt Creator, open the Display_a_map.pro file and locate the following lines and update PATH_TO_TOOLKIT variable with the path of the toolkitcpp.pri file (highlighted in yellow) or the OAuth dialog will not appear to enter your user credentials. Then save the file.
ARCGIS_RUNTIME_VERSION = 300.0.0include($$PWD/arcgisruntime.pri)
# TODO: You need to replace the <path_to_toolkit_repo> with a valid location where the Qt Toolkit# resides on your system, example: C:/arcgis-maps-sdk-toolkit-qt/uitools/toolkitcpp/toolkitcpp.pri# This block determines whether you've cloned your toolkitPATH_TO_TOOLKIT = "<path_to_toolkit_repo>/uitools/toolkitcpp/toolkitcpp.pri"
exists($${PATH_TO_TOOLKIT}) { message("Toolkit found") DEFINES += TOOLKIT_FOUND
# include the toolkitcpp.pri, which contains all the toolkit resources include($${PATH_TO_TOOLKIT})
qtHaveModule(webenginequick) { QT += webenginequick }} else { message("Toolkit not found in provided path. Either set PATH_TO_TOOLKIT or use an API Key")}Set developer credentials in the solution
-
In the project Sources folder of Qt Creator, open the Display_a_map.cpp file.
-
Set your values for the REDIRECT_URL and the CLIENT_ID strings (highlighted in yellow). Then save the file.
{
#ifdef TOOLKIT_FOUND
// Define the Redirect URL string obtained when creating the OAuth credentials. // TODO: You need to replace the "REDIRECT_URL" with your own valid string, // ex: "urn:ietf:wg:oauth:2.0:oob" const auto qStringRedirectUrl = QString{"REDIRECT_URL"};
// Define the URL of the portal to authenticate with. const QUrl qUrlPortal = QUrl{"https://www.arcgis.com/"};
// Define a unique identifier associated with an application registered with the // portal that assists with client/server OAuth authentication. // TODO: You need to replace the "CLIENT_ID" with your own valid string. const QString qStringClientId = QString{"CLIENT_ID"};
// Create a new OAuth user configuration using: the Url to mapping web service, the Client ID // string, and the Redirect Url string. auto* oAuthUserConfiguration = new OAuthUserConfiguration(qUrlPortal, qStringClientId, qStringRedirectUrl, this);
// Call the Toolkit's OAuthUserConfigurationManager static `addConfiguration` // method to use the OAuth dialog. This will tell the Authenticator to use OAuth for the provided configuration. Toolkit::OAuthUserConfigurationManager::addConfiguration(oAuthUserConfiguration);#endif // TOOLKIT_FOUNDRun the app
Press Ctrl + R to run the app.
You should see a map
What’s next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: