Skip to content

This sample demonstrates how to query related features from a FeatureLayer by using the queryRelatedFeatures() method. Each hexagon on the map represents one or more major cities in the U.S. Clicking on a hexagon will trigger a query for its related features, which will be displayed in a table added to the UI.

How it works

A function calls queryObjectIds() whenever the user clicks on the map, which returns the objectId of the corresponding hexagon in the layer.

const objectIds = await layer.queryObjectIds({
geometry: event.mapPoint,
spatialRelationship: "intersects",
returnGeometry: false,
outFields: ["*"],
});

We highlight the hexagon, then use the queryRelatedFeatures() method to query for the related features attached to this object id.

const relatedFeatureSetByObjectId = await layer.queryRelatedFeatures({
outFields: ["NAME", "SUM_POPULATION"],
relationshipId: layer.relationships[0].id,
objectIds: objectIds,
});

Then, we get the attributes from the resulting FeatureSet and create a table component that holds the resulting data.