Search for an address

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

search for an address

Geocoding is the process of converting address or place text into a location. The Geocoding service can search for an address or a place and perform reverse geocoding.

In this tutorial, you use a search bar in the user interface to access the Geocoding service and search for addresses and places.

To learn how to use the Geocoding service to reverse geocode, visit the Reverse geocode tutorial.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.
  3. The ArcGIS Runtime API for Qt is installed.

Steps

Open the project in Qt Creator

  1. To start this tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the display_a_map project in Qt Creator.

  3. If you downloaded the Display a map solution, set your API key.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In the Projects window, in the Sources folder, open the main.cpp file.

    3. Modify the code to set the API key. Paste the API key, acquired from your dashboard, between the quotes. Then save and close the file.

      main.cpp
      39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 40 41 42 43 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44
      Add line.Add line.Add line.Add line.Add line.
      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
      //   Copyright 2020 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
      //
      //   https://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.
      //
      
      #include "Display_a_map.h"
      
      #include "ArcGISRuntimeEnvironment.h"
      #include "MapQuickView.h"
      
      #include <QDir>
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      //------------------------------------------------------------------------------
      
      using namespace Esri::ArcGISRuntime;
      
      int main(int argc, char *argv[])
      {
          QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
      
          // Use of Esri location services, including basemaps and geocoding, requires
          // either an ArcGIS identity or an API key. For more information see
          // https://links.esri.com/arcgis-runtime-security-auth.
      
          // 1. ArcGIS identity: An ArcGIS named user account that is a member of an
          // organization in ArcGIS Online or ArcGIS Enterprise.
      
          // 2. API key: A permanent key that gives your application access to Esri
          // location services. Create a new API key or access existing API keys from
          // your ArcGIS for Developers dashboard (https://links.esri.com/arcgis-api-keys).
      
          const QString apiKey = QStringLiteral("");
      
          if (apiKey.isEmpty())
          {
              qWarning() << "Use of Esri location services, including basemaps, requires "
                            "you to authenticate with an ArcGIS identity or set the API Key property.";
          }
          else
          {
              ArcGISRuntimeEnvironment::setApiKey(apiKey);
          }
      
          // Production deployment of applications built with ArcGIS Runtime requires you to
          // license ArcGIS Runtime functionality. For more information see
          // https://links.esri.com/arcgis-runtime-license-and-deploy.
      
          // ArcGISRuntimeEnvironment::setLicense("Place license string in here");
      
          // Register the map view for QML
          qmlRegisterType<MapQuickView>("Esri.display_a_map", 1, 0, "MapView");
      
          // Register the Display_a_map (QQuickItem) for QML
          qmlRegisterType<Display_a_map>("Esri.display_a_map", 1, 0, "Display_a_map");
      
          // Initialize application view
          QQmlApplicationEngine engine;
      
          // Add the import Path
          engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
      
          // Set the source
          engine.load(QUrl("qrc:/qml/main.qml"));
      
          return app.exec();
      }
      
      //------------------------------------------------------------------------------

Declare classes, member variables, functions, and signals

  1. Double click on Sources > Display_a_map.h to open the file. Include the six header files below.

    Display_a_map.h
    21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 23 24 25 26 27 28 29 30 30 30 30 30 30 29 28 27 27 27 27 27 27 26 25 25 25 25 25 24 23 22 21 21 21 21 20 19 18 18 18 18 18 18 17 16 16 16 16 15 14 13 12 11 10 9 8 7 6 6 6 6
    Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  2. Add the following two include statements.

    Display_a_map.h
    31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 32 33 34 35 36 37 37 37 37 37 37 37 36 35 35 35 35 35 34 33 32 31 31 31 31 30 29 28 28 28 28 28 28 27 26 26 26 26 25 24 23 22 21 20 19 18 17 16 16 16 16
    Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  3. Add a Q_PROPERTY for the member variable suggestions.

    Display_a_map.h
    38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 39 40 41 42 43 44 44 44 44 44 44 43 42 41 40 40 40 40 39 38 37 37 37 37 37 37 36 35 35 35 35 34 33 32 31 30 29 28 27 26 25 25 25 25
    Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
    
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  4. Add the following public member functions with the Q_INVOKABLE macro to allow these to be invokable from the GUI.

    Display_a_map.h
    45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 46 47 48 49 50 51 52 52 52 52 52 51 50 49 49 49 49 49 49 48 47 47 47 47 46 45 44 43 42 41 40 39 38 37 37 37 37
    Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  5. Add two new signal declarations.

    Display_a_map.h
    53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 54 55 56 57 58 58 58 58 58 58 58 57 56 56 56 56 55 54 53 52 51 50 49 48 47 46 46 46 46
    Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  6. Declare the setupLocatorTask private method.

    Display_a_map.h
    59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 60 61 62 63 64 65 65 65 65 65 64 63 62 61 60 59 58 57 56 55 55 55 55
    Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H
  7. Add the following six private member functions and variables. Then save and close the header file.

    Display_a_map.h
    64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 78 78 78 78
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    class MapQuickView;
    
    class GraphicsOverlay;
    class Graphic;
    class LocatorTask;
    class GeocodeResult;
    class SuggestResult;
    class TextSymbol;
    
    }
    }
    #include <QObject>
    
    #include <QAbstractListModel>
    #include "GeocodeParameters.h"
    
    class Display_a_map : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
      Q_PROPERTY(QAbstractListModel* suggestions READ suggestions NOTIFY suggestionsChanged)
    public:
      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);
    
    signals:
      void mapViewChanged();
    
      void suggestionsChanged();
      void hideSuggestionView();
    
    private:
      Esri::ArcGISRuntime::MapQuickView* mapView() const;
      void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
      void setupMap();
    
      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;
    
    };
    
    #endif // DISPLAY_A_MAP_H

Include header files to access needed classes

  1. In Projects, double click on Sources > Display_a_map.cpp to open the file and add the following header file declarations.

    Display_a_map.cpp
    14 14 14 14 14 14 14 14 14 14 14 14 14 14 15 16 17 18 19 20 21 22 23 24 25 25 25 25 24 23 22 22 22 22 22 22 22 22 21 20 20 20 20 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -17 -17 -17 -17 -17 -17 -17 -17 -17 -17 -18 -19 -20 -21 -22 -22 -22 -22 -22 -23 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -24 -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
    Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    
  2. Include the following Qt classes.

    Display_a_map.cpp
    26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 27 28 29 30 30 30 30 30 30 30 30 30 29 28 28 28 28 28 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -10 -11 -12 -13 -14 -14 -14 -14 -14 -15 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -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
    Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    

Create a locator task with geocode parameters

Geocoding is implemented with a locator, typically created by referencing a service such as the Geocoding service or, for offline geocoding, by referencing locator data contained in a mobile package. Geocoding parameters can be used to fine-tune the results, such as setting the maximum number of results or requesting additional attributes in the results.

  1. Add the call to setupLocatorTask. This method will be implemented next.

    Display_a_map.cpp
    33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 34 35 36 37 38 39 39 39 39 39 39 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 2 2 2 2 2 2 2 2 2 2 1 0 -1 -2 -3 -3 -3 -3 -3 -4 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -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
    Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    
  2. Add code to begin implementing the setupLocatorTask method and initialize the auto-suggestion list. Within the method body, create a new LocatorTask with the Geocoding service URL, and set it to the m_locatorTask member variable.

    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 involves finding matching locations for a given address. Reverse-geocoding is the opposite and finds the closest address for a given location.

  3. Create a SuggestParameters, instance and initialize it with three categories as shown. Then set max results to limit for the number of returned suggestion results to 5.

  4. Configure GeocodeParams, (m_geocodeParams). Add code to set the minimum match score and the attribute names for the results to be returned. Call suggestions on m_locatorTask and assign to m_suggestListModel.

    Display_a_map.cpp
    38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 39 39 39 39 39 39 39 39 39 39 38 37 36 35 34 34 34 34 34 33 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    
  5. Add code to the setupLocatorTask to connect to the geocode complete signal on the LocatorTask. Within the lambda body if statement, 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.

  6. The second code block in the if statement sets text symbol, sets the text graphic geometry for the geocoding result display location, sets attributes for the text graphic, and displays the graphic.

    Display_a_map.cpp
    56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 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 81 81 81 81 81 81 81 81 81 81 81 80 79 78 77 76 76 76 76 76 75 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    

Create the auto-suggestion feature

  1. Add a new setSuggestions 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.cpp
    116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 115 114 113 112 111 111 111 111 111 110 109 109 109 109 109 109 109 109 109 109 109 109 109 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    //   Copyright 2020 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
    //
    //   https://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.
    
    
    #include "Display_a_map.h"
    #include "ArcGISRuntimeEnvironment.h"
    #include "Map.h"
    #include "MapQuickView.h"
    
    #include "SimpleRenderer.h"
    #include "SimpleMarkerSymbol.h"
    #include "TextSymbol.h"
    #include "LocatorTask.h"
    #include "SuggestResult.h"
    #include "SuggestListModel.h"
    
    #include <QUrl>
    
    #include <QAbstractListModel>
    #include <QGeoPositionInfoSource>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
      QObject(parent),
      m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    {
    
      setupLocatorTask();
    
    }
    Display_a_map::~Display_a_map()
    {
    }
    
    void Display_a_map::setupLocatorTask()
    {
      const QUrl geocode_service("https://geocode.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();
    
      // connect to geocode complete signal on the LocatorTask
      connect(m_locatorTask, &LocatorTask::geocodeCompleted, this, [this](QUuid, const QList<GeocodeResult>& geocodeResults)
      {
        if (!geocodeResults.isEmpty() && m_graphicResultLocation)
        {
          // display geocode result label and position
          const GeocodeResult geocodeResult = geocodeResults.at(0);
          m_graphicResultLocation->setGeometry(geocodeResult.displayLocation());
          m_graphicResultLocation->attributes()->setAttributesMap(geocodeResult.attributes());
          constexpr double scale = 8000.0;
          m_mapView->setViewpointCenter(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);
        }
      });
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
      return m_mapView;
    }
    
    // modified setupMap to handle mouseevent
    
    void Display_a_map::setupMap()
    {
    
      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->setViewpoint(viewpoint);
    
      configureGraphic();
    
    }
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
      if (!mapView || mapView == m_mapView)
      {
        return;
      }
    
      m_mapView = mapView;
      m_mapView->setMap(m_map);
    
      setupMap();
      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);
    }
    
    void Display_a_map::configureGraphic()
    {
      if (m_graphicResultLocation)
        return;
    
      // create graphics overlay and add to map view
      m_graphicsOverlay = new GraphicsOverlay(this);
    
      // set a renderer on the graphics overlay
      SimpleRenderer* 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 location
      m_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);
    }
    
    void Display_a_map::geocode(const QString& query)
    {
      m_locatorTask->geocodeWithParameters(query, m_geocodeParams);
    }
    
    QAbstractListModel* Display_a_map::suggestions() const
    {
      return m_suggestListModel;
    }
    
    void Display_a_map::clearGraphics()
    {
      m_graphicResultLocation->setGeometry(Point());
      m_graphicResultText->setGeometry(Point());
    }
    
  2. Revise the setupMap method to respond to the user's mouse selection of an address from the list of suggested addresses.

    Display_a_map.cpp
    89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 90 91 92 93 94 95 96 96 96 96 96 96 95 94 94 94 94 94 94 94 94 94 94 94 94