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.geocodeWithParameters(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.geocodeWithParameters(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.qmlFindPlace.qmlSearchBox.qmlSearchButton.qmlSuggestionView.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
// [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]

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtPositioning 5.6
import Esri.ArcGISRuntime 100.15
import Esri.ArcGISRuntime.Toolkit 100.15

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property bool isSearchingLocation: false
    property bool searchByExtent: false

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        onDrawStatusChanged: {
            if (drawStatus !== Enums.DrawStatusCompleted || mapView.locationDisplay.started)
                return;

            mapView.locationDisplay.autoPanMode = Enums.LocationDisplayAutoPanModeRecenter;
            mapView.locationDisplay.start();
        }

        Component.onDestruction: {
            mapView.locationDisplay.stop();
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISTopographic
            }

            // start the location display once the map loads
        }

        // add a graphics overlay to the mapview
        GraphicsOverlay {
            id: graphicsOverlay

            // create a renderer for graphics representing geocode results
            SimpleRenderer {
                PictureMarkerSymbol {
                    id: pinSymbol
                    height: 36
                    width: 19
                    url: "qrc:/Samples/Search/FindPlace/red_pin.png"
                    offsetY: height / 2
                }
            }
        }

        // declare a Callout
        Callout {
            id: callout
            calloutData: parent.calloutData
            accessoryButtonHidden: true
            screenOffsetY: (pinSymbol.height / 2) * -1
            leaderPosition: leaderPositionEnum.Automatic
        }

        // dismiss suggestions if a mouse press occurs in the mapview
        onMousePressed: {
            suggestionView.visible = false;
        }

        // dismiss suggestions and set a search flag when the current viewpoint changes
        onViewpointChanged: {
            suggestionView.visible = false;
            if (poiTextField.text.length > 0 && graphicsOverlay.graphics.count > 0)
                searchByExtent = true;
        }

        // perform an identify on mouse clicked
        onMouseClicked: {
            callout.dismiss();
            mapView.identifyGraphicsOverlayWithMaxResults(graphicsOverlay, mouse.x, mouse.y, 5, false, 1);
        }

        // display the callout with the identify result
        onIdentifyGraphicsOverlayStatusChanged: {
            if (identifyGraphicsOverlayStatus === Enums.TaskStatusCompleted){
                if (!identifyGraphicsOverlayResult.graphics.length > 0)
                    return;

                mapView.calloutData.geoElement = identifyGraphicsOverlayResult.graphics[0];
                mapView.calloutData.title = identifyGraphicsOverlayResult.graphics[0].attributes.attributeValue("ShortLabel");
                mapView.calloutData.detail = identifyGraphicsOverlayResult.graphics[0].attributes.attributeValue("Place_addr");
                callout.showCallout();
            }
        }
    }

    // declare a locator task that uses the world geocoding service
    LocatorTask {
        id: locatorTask

        // An ArcGIS Developer API key is required to utilize the world geocoding service
        url: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"

        // setup suggestions parameters
        suggestions {
            // set the search text for which to obtain suggestion results
            searchText: poiTextField.focus ? poiTextField.text : locationTextField.text
            suggestParameters: SuggestParameters {
                maxResults: 5
                // the Points of Interest text box should use the POI category, and the location
                // text box should use the Populated Place category as filters
                categories: poiTextField.focus ? ["POI"] : ["Populated Place"]
            }
        }

        // handle the geocodeStatusChanged signal
        onGeocodeStatusChanged: {
            if (geocodeStatus === Enums.TaskStatusCompleted) {
                searchByExtent = false;

                // first determine if we are searching for the preferredSearchLocation
                if (isSearchingLocation) {
                    isSearchingLocation = false;
                    if (!geocodeResults.length > 0)
                        return;

                    const topLocation = geocodeResults[0];
                    geocodePOIs(poiTextField.text, topLocation.displayLocation);
                    return;
                }

                // create graphics for each geocode result
                if (geocodeResults.length > 0) {
                    graphicsOverlay.graphics.clear();
                    let bbox;
                    for (let i = 0; i < geocodeResults.length; i++) {
                        const graphic = ArcGISRuntimeEnvironment.createObject("Graphic");
                        graphic.geometry = geocodeResults[i].displayLocation;
                        graphic.attributes.attributesJson = geocodeResults[i].attributes;
                        graphicsOverlay.graphics.append(graphic);
                        // create bounding box so we can set the viewpoint at the end
                        if (bbox) {
                            bbox = GeometryEngine.unionOf(bbox, graphic.geometry);
                        } else {
                            bbox = graphic.geometry;
                        }
                    }
                    mapView.setViewpointGeometryAndPadding(bbox, 40);
                }

                else {
                    callout.dismiss();
                }
            }
        }
    }

    Rectangle {
        anchors {
            fill: searchColumn
            margins: -5
        }
        color: "white"
    }

    Column {
        id: searchColumn
        anchors {
            left: parent.left
            right: parent.right
            top: parent.top
            margins: 10
        }
        spacing: 3

        // create a text field for the POI search
        SearchBox {
            id: poiTextField
            imageUrl: "qrc:/Samples/Search/FindPlace/find.png"
            placeholderText: "Point of interest (e.g. Movie Theater)"
            onTextChanged: if (text.length > 0 && suggestionView) { suggestionView.visible = true; }
            onAccepted: {
                geocodePOIs(poiTextField.text, locationTextField.text);
                suggestionView.visible = false;
                callout.dismiss();
            }
            onImageClicked: {
                geocodePOIs(poiTextField.text, locationTextField.text);
                suggestionView.visible = false;
                callout.dismiss();
            }
        }

        // create a text field for the location search
        SearchBox {
            id: locationTextField
            imageUrl: "qrc:/Samples/Search/FindPlace/location.png"
            placeholderText: "In proximity of"
            text: "Current Location"
            onTextChanged: if (text.length > 0 && suggestionView) { suggestionView.visible = true; }
            onAccepted: {
                geocodePOIs(poiTextField.text, locationTextField.text);
                suggestionView.visible = false;
            }
            onImageClicked: {
                locationTextField.text = "Current Location";
                geocodePOIs(poiTextField.text, locationTextField.text);
                suggestionView.visible = false;
            }
        }

        // create a list view for the suggestion results
        SuggestionView {
            id: suggestionView
            width: parent.width
            height: 20 * locatorTask.suggestions.count
            onSuggestionClicked: {
                if (locatorTask.geocodeStatus !== Enums.TaskStatusInProgress) {
                    // change the text label
                    poiTextField.focus ? poiTextField.text = label : locationTextField.text = label;

                    // geocode
                    geocodePOIs(poiTextField.text, locationTextField.text);
                }

                // dismiss suggestions
                suggestionView.visible = false;
            }
        }
    }

    // create a button that allows the user to re-search the current map extent
    SearchButton {
        id: searchExtentButton
        anchors {
            horizontalCenter: parent.horizontalCenter
            bottom: parent.bottom
            bottomMargin: 23
        }
        visible: searchByExtent
        onButtonClicked: {
            geocodePOIs(poiTextField.text, null, mapView.currentViewpointExtent.extent);
            callout.dismiss();
        }
    }

    function geocodePOIs(poi, location, extent) {
        // create base geocode parameters
        const geocodeParams = ArcGISRuntimeEnvironment.createObject("GeocodeParameters");
        geocodeParams.resultAttributeNames = ["*"];
        geocodeParams.maxResults = 50;
        geocodeParams.minScore = 75.0;

        // if extent is not null, use this as the searchArea filter in the parameters
        if (extent) {
            // setup the parameters to filter by results near the preferredSearchLocation
            geocodeParams.searchArea = extent;
            geocodeParams.outputSpatialReference = extent.spatialReference;

            // execute the geocode
            locatorTask.geocodeWithParameters(poi, geocodeParams);
            return;
        }

        // if extent and location are null, do a generic geocode with no spatial filter
        if (!location) {
            locatorTask.geocodeWithParameters(poi, geocodeParams);
            return;
        }

        // check if the provided location is a Point
        if (location.x) {
            // setup the parameters to filter by results near the preferredSearchLocation
            geocodeParams.preferredSearchLocation = location;
            geocodeParams.outputSpatialReference = location.spatialReference;

            // execute the geocode with parameters
            locatorTask.geocodeWithParameters(poi, geocodeParams);
        // Check if a the location display's location should be used
        } else if (location === "Current Location") {
            geocodeParams.preferredSearchLocation = mapView.locationDisplay.location.position;
            geocodeParams.outputSpatialReference = mapView.spatialReference;

            // execute the geocode with parameters
            locatorTask.geocodeWithParameters(poi, geocodeParams);
        } else {
            // we only have the name of a location, so we must geocode to get a Point for the preferredSearchLocation
            isSearchingLocation = true;
            locatorTask.geocodeWithParameters(location, geocodeParams);
        }
    }
}

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