Search for web map by keyword

View inC++QMLView on GitHubSample viewer app

Find webmap portal items by using a search term.

screenshot

Use case

Portals can contain many portal items and at times you may wish to query the portal to find what you're looking for. In this example, we search for webmap portal items using a text search.

How to use the sample

Enter search terms into the search bar. Once the search is complete, a list is populated with the resultant webmaps. Tap on a webmap to set it to the map view. For more results, click the "More Results" button at the bottom.

How it works

  1. Create a new Portal and load it.
  2. Create new PortalQueryParametersForItems. Set the type to PortalItemType::WebMap and add the text you want to search for.
  3. Use portal::findItems(params) to get the first set of matching items (10 by default).
  4. Get more results with portal::findItems(PortalQueryResultSetForItems.nextQueryParameters()).

Relevant API

  • Portal
  • PortalItem
  • PortalQueryParameters
  • PortalQueryResultSet

Tags

keyword, query, search, webmap

Sample Code

SearchForWebmap.cppSearchForWebmap.cppSearchForWebmap.hSearchForWebmap.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// [WriteFile Name=SearchForWebmap, Category=CloudAndPortal]
// [Legal]
// Copyright 2016 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "AuthenticationManager.h"
#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "Portal.h"
#include "PortalItem.h"
#include "PortalItemListModel.h"
#include "PortalQueryParametersForItems.h"
#include "SearchForWebmap.h"

#include <QDate>

using namespace Esri::ArcGISRuntime;

SearchForWebmap::SearchForWebmap(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_portal(new Portal(this))
{
  AuthenticationManager::instance()->setCredentialCacheEnabled(false);
}

SearchForWebmap::~SearchForWebmap() = default;

void SearchForWebmap::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<SearchForWebmap>("Esri.Samples", 1, 0, "SearchForWebmapSample");
}

void SearchForWebmap::componentComplete()
{
  QQuickItem::componentComplete();

  if (m_portal)
  {
    connect(m_portal, &Portal::loadStatusChanged, this, [this]()
    {
      m_portalLoaded = m_portal->loadStatus() == LoadStatus::Loaded;
      emit portalLoadedChanged();
    });

    //! [SearchForWebmap CPP Portal find items completed]
    connect(m_portal, &Portal::findItemsCompleted, this, [this](PortalQueryResultSetForItems *webmapResults)
    {
      m_webmapResults = webmapResults;
      m_webmaps = m_webmapResults->itemResults();
      emit webmapsChanged();
      emit hasMoreResultsChanged();
    });
    //! [SearchForWebmap CPP Portal find items completed]

    m_portal->load();
  }

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  if(m_mapView)
    m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);
}

bool SearchForWebmap::portalLoaded() const
{
  return m_portalLoaded;
}

QAbstractListModel* SearchForWebmap::webmaps() const
{
  return m_webmaps;
}

bool SearchForWebmap::hasMoreResults() const
{
  return m_webmapResults && m_webmapResults->nextQueryParameters().startIndex() > -1;
}

QString SearchForWebmap::mapLoadError() const
{
  return m_mapLoadeError;
}

void SearchForWebmap::search(const QString& keyword)
{
  if (!m_portal)
    return;

  //! [SearchForWebmap CPP Portal find items]
  // webmaps authored prior to July 2nd, 2014 are not supported
  // so search only from that date to the current time (in milliseconds)
  QString fromDate = QString("000000%1").arg(QDateTime(QDate(2014, 7, 2)).toMSecsSinceEpoch());
  QString toDate = QString("000000%1").arg(QDateTime::currentDateTime().toMSecsSinceEpoch());

  PortalQueryParametersForItems query;
  query.setSearchString(QString("tags:\"%1\" AND +uploaded:[%2 TO %3]")
                        .arg(keyword, fromDate, toDate));
  query.setTypes(QList<PortalItemType>() << PortalItemType::WebMap);

  m_portal->findItems(query);
  //! [SearchForWebmap CPP Portal find items]

  if(m_mapView)
    m_mapView->setVisible(false);
}

void SearchForWebmap::searchNext()
{
  if (!m_webmapResults || !m_portal)
    return;

  //! [Portal find with nextQueryParameters]
  PortalQueryParametersForItems nextQuery = m_webmapResults->nextQueryParameters();
  // check whether the startIndex of the new query is valid
  if (nextQuery.startIndex() != -1)
    m_portal->findItems(nextQuery);
  //! [Portal find with nextQueryParameters]
}

void SearchForWebmap::loadSelectedWebmap(int index)
{
  if (!m_webmaps)
     return;

   if (m_map)
     delete m_map;

   // create map from portal item
   m_map = new Map(m_webmaps->at(index), this);

   connect(m_map,&Map::errorOccurred, this, [this]()
   {
     m_mapLoadeError = m_map->loadError().message();
     emit mapLoadErrorChanged();
   });

   // set map on mapview
   m_mapView->setMap(m_map);
   m_mapView->setVisible(true);
}

void SearchForWebmap::errorAccepted()
{
  m_mapLoadeError.clear();
  emit mapLoadErrorChanged();
}

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