Search for web map by keyword

View 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::findItemsAsync(params) to get the first set of matching items (10 by default).
  4. Get more results with portal::findItemsAsync(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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// [WriteFile Name=SearchForWebmap, Category=CloudAndPortal]
// [Legal]
// Copyright 2016 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

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

// sample headers
#include "SearchForWebmap.h"

// ArcGIS Maps SDK headers
#include "AuthenticationManager.h"
#include "Error.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "Portal.h"
#include "PortalItem.h"
#include "PortalItemListModel.h"
#include "PortalQueryParametersForItems.h"
#include "PortalQueryResultSetForItems.h"
#include "PortalTypes.h"

// Qt headers
#include <QDate>
#include <QDateTime>
#include <QFuture>

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();
    });

    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 async]
  // 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::fromString(QDate(2014, 7, 2).toString()).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->findItemsAsync(query).then(
  [this](PortalQueryResultSetForItems* webmapResults)
  {
    onFindItemsCompleted(webmapResults);
  });
  //! [SearchForWebmap CPP Portal find items async]

  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.isEmpty())
  {
    m_portal->findItemsAsync(nextQuery).then(
    [this](PortalQueryResultSetForItems* webmapResults)
    {
      onFindItemsCompleted(webmapResults);
    });
  }
  //! [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();
}

void SearchForWebmap::onFindItemsCompleted(PortalQueryResultSetForItems* webmapResults)
{
  m_webmapResults = webmapResults;
  m_webmaps = m_webmapResults->itemResults();
  emit webmapsChanged();
  emit hasMoreResultsChanged();
}

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