Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Identify graphics

Sample Viewer View Sample on GitHub

Display an alert message when a graphic is clicked.

Use case

A user may wish to select a graphic on a map to view relevant information about it.

How to use the sample

Select a graphic to identify it. You will see an alert message displayed.

How it works

  1. Create a GraphicsOverlay and add it to the MapView.
  2. Add a Graphic along with a SimpleFillSymbol to the graphics overlay.
  3. Create a Point from the location clicked on the map view by the user from the mouseClicked signal on the MapView.
  4. Identify the graphic on the map view with identifyGraphicsOverlay(graphicsOverlay, mouse.x, mouse.y, tolerance, returnPopupsOnly, max results).

Relevant API

  • Graphic
  • GraphicsOverlay
  • MapView

Tags

graphics, identify

Sample Code

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

Rectangle {
    width: 800
    height: 600

    // Declare a map view inside the rectangle
    MapView {
        id: mapView

        anchors.fill: parent

        // Nest a map inside of the map view
        Map {
            id: map
            // set the basemap
            BasemapTopographic {}
        }

        // Add a graphics overlay to the map view
        GraphicsOverlay {
            id: graphicsOverlay
            // assign a render to the graphics overlay
            renderer: SimpleRenderer {
                symbol: SimpleFillSymbol {
                    style: Enums.SimpleFillSymbolStyleSolid
                    color: Qt.rgba(1, 1, 0, 0.7)
                }
            }
        }

        //! [identify graphics api snippet]
        // Signal handler for mouse click event on the map view
        onMouseClicked: {
            const tolerance = 22;
            const returnPopupsOnly = false;
            const maximumResults = 1000;
            mapView.identifyGraphicsOverlayWithMaxResults(graphicsOverlay, mouse.x, mouse.y, tolerance, returnPopupsOnly, maximumResults);
        }

        // Signal handler for identify graphics overlay
        onIdentifyGraphicsOverlayStatusChanged: {
            if (identifyGraphicsOverlayStatus === Enums.TaskStatusCompleted) {
                if (identifyGraphicsOverlayResult.graphics.length > 0) {
                    msgDialog.open();
                }
            } else if (identifyGraphicsOverlayStatus === Enums.TaskStatusErrored) {
                console.log("error");
            }
        }
        //! [identify graphics api snippet]
    }

    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: "Tapped on graphic"
        }
    }

    Component.onCompleted: {
        // create the polygon by assigning points
        const polygonBuilder = ArcGISRuntimeEnvironment.createObject("PolygonBuilder", {spatialReference: Factory.SpatialReference.createWebMercator()});
        polygonBuilder.addPointXY(-20e5, 20e5);
        polygonBuilder.addPointXY(20e5, 20e5);
        polygonBuilder.addPointXY(20e5, -20e5);
        polygonBuilder.addPointXY(-20e5, -20e5);
        // assign the geometry of the graphic to be the polygon
        const polygonGraphic = ArcGISRuntimeEnvironment.createObject("Graphic");
        polygonGraphic.geometry = polygonBuilder.geometry;
        // add the graphic to the polygon graphics overlay
        graphicsOverlay.graphics.append(polygonGraphic);
    }
}