Find a place

View inC++QMLView on GitHubSample viewer app

Find places of interest near a location or within a specific area.

screenshot

Use case

When getting directions or looking for nearby places, users may only know what the place has ("food"), the type of place ("gym"), or the generic place name ("Starbucks"), rather than the specific address. You can get suggestions and locations for these places of interest (POIs) using a natural language query. Additionally, you can filter the results to a specific area.

How to use the sample

Choose a place of interest to enter in the first field and an area to search within in the second field. Click the magnifying glass or hit enter to search and show the results of the query on the map from your current extent. Click on a result pin to show its name and address. If you pan away from the result area, a "Redo search in this area" button will appear. Click it to query again for the currently viewed area on the map.

How it works

  1. Create a LocatorTask using a URL to a locator service.
  2. Find the location for an address (or city name) to build an envelope to search within:
    • Create GeocodeParameters.
    • Add return fields to the parameters' resultAttributeNames collection. Only add a single "*" option to return all fields.
    • Call locatorTask::geocodeWithParametersAsync(locationQueryString, geocodeParameters) to get a list of GeocodeResults.
    • Use the displayLocation from one of the results to build an Envelope to search within.
  3. Get place of interest (POI) suggestions based on a place name query:
    • Create SuggestParameters.
    • Add "POI" to the parameters' categories collection.
    • Call locatorTask::suggestions() to get a list of SuggestResults.
    • The SuggestResult will have a label to display in the search suggestions list.
  4. Use one of the suggestions or a user-written query to find the locations of POIs:
    • Create GeocodeParameters.
    • Set the parameters' searchArea to the envelope.
    • Call locatorTask::geocodeWithParametersAsync(suggestionLabelOrPlaceQueryString, geocodeParameters) to get a list of GeocodeResults.
    • Display the places of interest using the results' displayLocations.

Relevant API

  • GeocodeParameters
  • GeocodeResult
  • LocatorTask
  • SuggestParameters
  • SuggestResult

About the data

This sample uses the World Geocoding Service.

Tags

businesses, geocode, locations, locator, places of interest, POI, point of interest, search, suggestions

Sample Code

FindPlace.cppFindPlace.cppFindPlace.hSearchBox.qmlSearchButton.qmlSuggestionView.qmlFindPlace.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// [WriteFile Name=FindPlace, Category=Search]
// [Legal]
// Copyright 2017 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

#include "FindPlace.h"

#include "CalloutData.h"
#include "Map.h"
#include "MapQuickView.h"
#include "GraphicsOverlay.h"
#include "GeocodeResult.h"
#include "GeoElement.h"
#include "GeometryEngine.h"
#include "PictureMarkerSymbol.h"
#include "IdentifyGraphicsOverlayResult.h"
#include "Point.h"
#include "SimpleRenderer.h"
#include "LocatorTask.h"
#include "LocationDisplay.h"
#include "SuggestListModel.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "GraphicsOverlayListModel.h"
#include "GraphicListModel.h"
#include "AttributeListModel.h"
#include "SuggestParameters.h"
#include "Location.h"
#include "Graphic.h"
#include "Viewpoint.h"
#include "SpatialReference.h"
#include "GeocodeResult.h"

#include <QFuture>
#include <QUrl>
#include <QUuid>

using namespace Esri::ArcGISRuntime;

FindPlace::FindPlace(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

FindPlace::~FindPlace()
{
  m_mapView->locationDisplay()->stop();
}

void FindPlace::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<FindPlace>("Esri.Samples", 1, 0, "FindPlaceSample");
  qmlRegisterUncreatableType<CalloutData>("Esri.Samples", 1, 0, "CalloutData", "CalloutData is an uncreatable type");
  qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");
}

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

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a map using the topographic basemap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);

  // Set map to map view
  m_mapView->setMap(m_map);

  // create the locator
  createLocator();

  // add the graphics overlay
  addGraphicsOverlay();

  // initialize callout
  m_mapView->calloutData()->setVisible(false);
  m_calloutData = m_mapView->calloutData();

  // connect mapview signals
  connectSignals();
}

void FindPlace::connectSignals()
{
  connect(m_mapView, &MapQuickView::drawStatusChanged, this, [this](DrawStatus drawStatus)
  {
    if (drawStatus != DrawStatus::Completed || m_mapView->locationDisplay()->isStarted())
      return;

    // turn on the location display
    m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Recenter);
    m_mapView->locationDisplay()->start();
  });

  connect(m_mapView, &MapQuickView::mousePressed, this, [this](QMouseEvent& /*event*/)
  {
    emit hideSuggestionView();
  });

  connect(m_mapView, &MapQuickView::viewpointChanged, this, [this]()
  {
    emit hideSuggestionView();
    if (m_graphicsOverlay->graphics()->size() > 0)
      emit showExtentButton();
  });

  // perform an identify operation on mouse clicked
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this](QMouseEvent& e)
  {
    emit hideCallout();
    m_mapView->identifyGraphicsOverlayAsync(m_graphicsOverlay, e.position(), 5, false, 1).then(this, [this] (IdentifyGraphicsOverlayResult* result)
    {
      if (result->graphics().length() == 0)
        return;

      // display the callout with the identify result
      Graphic* graphic = result->graphics().at(0);
      m_calloutData->setGeoElement(graphic);
      m_calloutData->setTitle(graphic->attributes()->attributeValue("ShortLabel").toString());
      m_calloutData->setDetail(graphic->attributes()->attributeValue("Place_addr").toString());
      emit showCallout();
    });
  });
}

void FindPlace::addGraphicsOverlay()
{
  // add a graphics overlay to the mapview
  m_graphicsOverlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

  // create a renderer for graphics representing geocode results
  PictureMarkerSymbol* pinSymbol = new PictureMarkerSymbol(QUrl("qrc:/Samples/Search/FindPlace/red_pin.png"), this);
  pinSymbol->setHeight(36);
  pinSymbol->setWidth(19);
  pinSymbol->setOffsetY(pinSymbol->height() / 2);
  SimpleRenderer* simpleRenderer = new SimpleRenderer(pinSymbol, this);
  m_graphicsOverlay->setRenderer(simpleRenderer);
}

void FindPlace::createLocator()
{
  // create a locator task that uses the world geocoding service
  // an ArcGIS Developer API key is required to utilize the world geocoding service
  m_locatorTask = new LocatorTask(QUrl("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"), this);

  // set the suggestions Q_PROPERTY
  m_suggestListModel = m_locatorTask->suggestions();
  emit suggestionsChanged();
}

void FindPlace::onGeocodingCompleted_(const QList<GeocodeResult>& results)
{
  // if we are converting the location string to a Point, re-geocode with that information,
  // and don't add any graphics to the map
  if (m_isSearchingLocation)
  {
    m_isSearchingLocation = false;
    if (results.length() == 0)
      return;

    GeocodeResult topLocation = results.at(0);
    geocodePOIs(m_poiSearchText, topLocation.displayLocation());
    return;
  }

  // create graphics for each geocode result
  if (results.length() == 0)
    return;

  m_graphicsOverlay->graphics()->clear();

  // delete parent if it exists
  if (m_graphicParent)
  {
    delete m_graphicParent;
    m_graphicParent = nullptr;
  }
  m_graphicParent = new QObject(this);

  Geometry bbox;
  for (const GeocodeResult& result : results)
  {
    Graphic* graphic = new Graphic(result.displayLocation(), result.attributes(), m_graphicParent);
    m_graphicsOverlay->graphics()->append(graphic);
    // create bounding box so we can set the viewpoint at the end
    if (bbox.isEmpty())
      bbox = graphic->geometry();
    else
      bbox = GeometryEngine::unionOf(bbox, graphic->geometry());
  }
  // zoom to the bounding box
  m_mapView->setViewpointGeometryAsync(bbox, 40);
}

void FindPlace::setPoiTextHasFocus(bool hasFocus)
{
  if (m_poiTextHasFocus == hasFocus)
    return;

  m_poiTextHasFocus = hasFocus;

  // create and update the suggestion parameters
  SuggestParameters suggestParams;
  // the Points of Interest suggestions should use the POI category, and the location
  // suggestions should use the Populated Place category as filters
  QStringList categories;
  m_poiTextHasFocus ? categories << "POI" : categories << "Populated Place";
  suggestParams.setCategories(categories);
  suggestParams.setMaxResults(5);
  if (m_locatorTask)
    m_locatorTask->suggestions()->setSuggestParameters(suggestParams);
  emit poiTextHasFocusChanged();
}

// set the suggestion search text to fetch suggestions
void FindPlace::setSuggestionsText(const QString& searchText)
{
  if (!m_suggestListModel)
    return;

  SuggestListModel* suggestListModel = dynamic_cast<SuggestListModel*>(m_suggestListModel);
  if (!suggestListModel)
    return;

  suggestListModel->setSearchText(searchText);
}

// geocode without any spatial filter
void FindPlace::geocodePOIs(const QString& poi)
{
  m_poiSearchText = poi;
  GeocodeParameters params = createParameters();
  m_locatorTask->geocodeWithParametersAsync(poi, params).then(this, [this](const QList<GeocodeResult>& results)
  {
    onGeocodingCompleted_(results);
  });
}

// use extent to filter the geocode results
void FindPlace::geocodePOIs(const QString& poi, SearchMode mode)
{
  m_poiSearchText = poi;

  GeocodeParameters params = createParameters();

  // If the Mode is CurrentLocation, use the Location Display as preferred search location
  if (mode == SearchMode::CurrentLocation)
  {
    params.setPreferredSearchLocation(m_mapView->locationDisplay()->location().position());
    params.setOutputSpatialReference(m_mapView->locationDisplay()->location().position().spatialReference());

    m_locatorTask->geocodeWithParametersAsync(m_poiSearchText, params).then(this, [this](const QList<GeocodeResult>& results)
    {
      onGeocodingCompleted_(results);
    });
  }
  // If the Mode is BoundingGeometry, use the MapView's current viewpoint as the search area
  else
  {
    GeocodeParameters params = createParameters();
    Geometry extent = m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry();
    params.setSearchArea(extent);
    params.setOutputSpatialReference(extent.spatialReference());

    m_locatorTask->geocodeWithParametersAsync(m_poiSearchText, params).then(this, [this](const QList<GeocodeResult>& results)
    {
      onGeocodingCompleted_(results);
    });
  }
}

// use a point as a preferred search location for the geocode results
void FindPlace::geocodePOIs(const QString& poi, const Point& location)
{
  m_poiSearchText = poi;

  GeocodeParameters params = createParameters();
  params.setPreferredSearchLocation(location);
  params.setOutputSpatialReference(location.spatialReference());

  m_locatorTask->geocodeWithParametersAsync(m_poiSearchText, params).then(this, [this](const QList<GeocodeResult>& results)
  {
    onGeocodingCompleted_(results);
  });
}

// use a location string as a preferred search location for the geocode results
void FindPlace::geocodePOIs(const QString& poi, const QString& location)
{
  if (location == m_currentLocationText)
  {
    geocodePOIs(poi, SearchMode::CurrentLocation);
    return;
  }
  m_poiSearchText = poi;
  GeocodeParameters params = createParameters();

  m_isSearchingLocation = true;
  m_locatorTask->geocodeWithParametersAsync(location, params).then(this, [this](const QList<GeocodeResult>& results)
  {
    onGeocodingCompleted_(results);
  });
}

// create base geocode parameters
GeocodeParameters FindPlace::createParameters()
{
  GeocodeParameters params;
  params.setResultAttributeNames(QStringList{"*"});
  params.setMaxResults(50);
  params.setMinScore(75.0);
  return params;
}

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