Find a place

View inQMLC++
View on GitHub
Sample 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.cppFindPlace.hSearchBox.qmlSearchButton.qmlSuggestionView.qml
                                                                                                                                                     
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
// [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.Toolkit 100.11
import Esri.Samples 1.0

FindPlaceSample {
    id: findPlaceSample
    clip: true
    width: 800
    height: 600

    property bool isSearchingLocation: false
    property bool searchByExtent: false

    onHideSuggestionView: suggestionView.visible = false
    onHideCallout: callout.dismiss();
    onShowCallout: callout.showCallout();
    onShowExtentButton: searchExtentButton.visible = true;

    // add a mapView component
    MapView {
        id: mapView
        anchors.fill: parent
        objectName: "mapView"

        // declare a Callout
        Callout {
            id: callout
            leaderPosition: leaderPositionEnum.Automatic
            calloutData: mapView.calloutData
            accessoryButtonHidden: true
            screenOffsetY: -19
        }
    }

    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;
                findPlaceSample.setSuggestionsText(text);
            }
            onFocusChanged: if (focus) { poiTextHasFocus = 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;
                findPlaceSample.setSuggestionsText(text);
            }
            onFocusChanged: if (focus) { poiTextHasFocus = false; }
            onAccepted: {
                geocodePOIs(poiTextField.text, locationTextField.text);
                suggestionView.visible = false;
            }
            onImageClicked: {
                locationTextField.text = "Current Location";
                geocodePOIs(poiTextField.text, FindPlaceSample.CurrentLocation);
                suggestionView.visible = false;
            }
        }

        // create a list view for the suggestion results
        SuggestionView {
            id: suggestionView
            width: parent.width
            height: 20 * suggestionView.count
            onSuggestionClicked: {
                // 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, FindPlaceSample.MapExtent);
            callout.dismiss();
        }
    }
}

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