Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Find a place

Sample Viewer View Sample on GitHub

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

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 locator service "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer".

Tags

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

Sample Code

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

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

    property bool isSearchingLocation: false
    property bool searchByExtent: false

    MapView {
        id: mapView
        anchors.fill: parent

        Map {
            BasemapTopographic {}

            // start the location display once the map loads
            onLoadStatusChanged: {
                if (loadStatus !== Enums.LoadStatusLoaded)
                    return;

                mapView.locationDisplay.start();
            }
        }

        // 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
        url: "https://geocode.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);
        }
    }
}
import QtQuick 2.6
import QtQuick.Controls 2.2

TextField {
    width: parent.width
    selectByMouse: true

    property url imageUrl
    signal imageClicked

    Rectangle {
        anchors {
            verticalCenter: parent.verticalCenter
            right: parent.right
            margins: 5
        }
        height: 22
        width: height

        Image {
            anchors {
                fill: parent
                margins: 2
            }
            source: imageUrl

            MouseArea {
                anchors.fill: parent
                onClicked: imageClicked()
            }
        }
    }
}
import QtQuick 2.6

Rectangle {
    property bool pressed: false
    signal buttonClicked()

    width: 175
    height: 35
    color: pressed ? "#959595" : "#D6D6D6"
    radius: 5
    border {
        color: "#585858"
        width: 1
    }

    Text {
        anchors.centerIn: parent
        text: "Redo search in this area"
        font.pixelSize: 14
        color: "#474747"
    }

    MouseArea {
        anchors.fill: parent
        onPressed: searchExtentButton.pressed = true
        onReleased: searchExtentButton.pressed = false
        onClicked: {
            buttonClicked();
        }
    }
}
import QtQuick 2.6

Rectangle {
    color: "#f7f8fa"
    opacity: 0.85

    signal suggestionClicked(var label)

    ListView {
        id: suggestView
        model: locatorTask.suggestions
        height: parent.height
        delegate: Component {

            Rectangle {
                width: poiTextField.width
                height: 20
                color: "#f7f8fa"
                border.color: "darkgray"

                Text {
                    anchors {
                        verticalCenter: parent.verticalCenter
                        leftMargin: 5
                        rightMargin: 5
                    }

                    font {
                        weight: Font.Black
                        pixelSize: 12
                    }

                    width: parent.width
                    text: label
                    elide: Text.ElideRight
                    leftPadding: 5
                    color: "black"
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        suggestionClicked(label)
                    }
                }
            }
        }
    }
}