Query Attachments

Explore in the sandboxView live

This sample demonstrates how to query attachments from a FeatureLayer through Beverly Hills tree data by using the queryObjectIDs() and queryAttachments() methods. When user clicks somewhere in the map, the attachments located within 800m of the click location will appear in the div on the left hand side.

How it works

A function is called whenever the user clicks on the map, which first calls a query for all object ids within 800m of the point where the map was clicked.

        
1
2
3
4
5
6
7
8
layer.queryObjectIds({
    geometry: point,
    spatialRelationship: "intersects",
    distance: 800,
    units: "meters",
    returnGeometry: false,
    outFields: ["*"]
})

This will return an array of object ids which we highlight to show the user the bounds of their query then pass to the queryAttachments() method to query for attachments on any of the objects returned from our first query.

    
1
2
3
4
layer.queryAttachments({
    attachmentTypes: ["image/jpeg"],
    objectIds: objectIds
});

This method will return an array of ids for the attachments returned from the query, which we display by creating an image element and appending it to the queryResults div in the side panel.

         
1
2
3
4
5
6
7
8
9
Object.keys(attachmentsByFeatureId).forEach(function(objectId) {
    const attachments = attachmentsByFeatureId[objectId];
    attachments.forEach(function (attachment) {
        const image = document.createElement("img");
        image.src = attachment.url;
        image.className = "queryImg";
        document.getElementById("queryResults").appendChild(image);
    });
});

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