Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Identify layers

Sample Viewer View Sample on GitHub

Identify features in all layers in a map.

Use case

IdentifyLayers allows users to tap on a map, returning features at that location across multiple layers. Because some layer types have sublayers, the sample recursively counts results for sublayers within each layer.

How to use the sample

Tap to identify features. An alert will show all layers with features under the cursor, as well as a layer count.

How it works

  1. The tapped position is passed to mapView.identifyLayersWithMaxResults
  2. For each IdentifyLayerResult in the results, features are counted.
    • Note: there is one identify result per layer with matching features; if the feature count is 0, that means a sublayer contains the matching features.

Relevant API

Additional information

The GeoView supports two methods of identify: identifyLayer(), which identifies features within a specific layer and identifyLayers(), which identifies features for all layers in the current view.

Tags

identify, recursion, recursive, sublayers

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.9

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

    property string msgText: ""

    MapView {
        id: mapView
        anchors.fill: parent

        Map {
            BasemapTopographic {}

            // add a map image layer and hide 2 of the sublayers
            ArcGISMapImageLayer {
                url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer"
                onLoadStatusChanged: {
                    if (error) {
                        console.log("error:", error.message, error.additionalMessage)
                        return;
                    }

                    if (loadStatus !== Enums.LoadStatusLoaded)
                        return;

                    mapImageSublayers.get(1).visible = false;
                    mapImageSublayers.get(2).visible = false;
                }
            }

            // add a feature layer
            FeatureLayer {
                ServiceFeatureTable {
                    url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"
                }
            }

            // set an initial viewpoint
            ViewpointCenter {
                targetScale: 68015210

                Point {
                    x: -10977012.785807
                    y: 4514257.550369
                    spatialReference: SpatialReference { wkid: 3857 }
                }
            }
        }

        // identify layers on mouse click
        onMouseClicked: {
            const screenX = mouse.x;
            const screenY = mouse.y;
            const tolerance = 12;
            const returnPopups = false;
            const maxResults = 10;
            mapView.identifyLayersWithMaxResults(screenX, screenY, tolerance, returnPopups, maxResults);
        }

        // handle the identify results
        onIdentifyLayersStatusChanged: {
            if (identifyLayersStatus !== Enums.TaskStatusCompleted)
                return;

            // reset the message string
            msgText = "";

            // loop through the results
            const results = mapView.identifyLayersResults;
            for (let i = 0; i < results.length; i++) {
                const result = results[i];
                const count = geoElementsCountFromResult(result);
                const layerName = result.layerContent.name;
                msgText += "%1 : %2".arg(layerName).arg(count);
                // add new line character if not the final element in array
                if (i !== results.length)
                    msgText += "\n";
            }

            // show the message box
            if (msgText.length > 0)
                msgDialog.open();
        }

        // handle any errors on the map view
        onErrorChanged: {
            if (error)
                console.log("error:", error.message, error.additionalMessage)
        }
    }

    function geoElementsCountFromResult(identifyLayerResult) {
        // create temp array
        const tempResults = [identifyLayerResult];

        // use Depth First Search approach to handle recursion
        let count = 0;
        let index = 0;

        while (index < tempResults.length) {
            //get the result object from the array
            const identifyResult = tempResults[index];

            // update count with geoElements from the result
            count += identifyResult.geoElements.length;

            // check if the result has any sublayer results
            // if yes then add those result objects in the tempResults
            // array after the current result
            if (identifyResult.sublayerResults.length > 0) {
                tempResults.push(identifyResult.sublayerResults[index]);
            }

            // update the count and repeat
            index += 1;
        }

        return count;
    }

    Dialog {
        id: msgDialog
        modal: true
        x: Math.round(parent.width - width) / 2
        y: Math.round(parent.height - height) / 2
        standardButtons: Dialog.Ok
        property alias text : textLabel.text
        Text {
            id: textLabel
            text: msgText
        }
    }
}