Select to view content in your preferred language

FeatureLayer modes (MODE_SELECTION and MODE_ONDEMAND) for JS 4.x

522
7
Jump to solution
09-13-2024 10:03 AM
Mr_Kirkwood
Occasional Contributor

I am new to the JS 4.x language. The JS 3.x had the modes. These helped with queries where you could zoom into a FeatureLayer  and have it turn on when zoomed to. The Featurelayer doesnt show up when using MODE_SELECTION until it was queried and selected. Here is an exapmple of the 3.x code:

floorOutline = new FeatureLayer (urlFloorOutline, {
                    mode: FeatureLayer.MODE_SELECTION,
                    id: 'floorOutline',
                    outFields: ["*"],
                    opacity: 0.8,
                });

This was added t othe map and did not show up on the map until you ran the zoom to fucntion:

function zoomFloorOutline (id)
{
    require ([
        "esri/tasks/query",
        "esri/geometry/ScreenPoint",
        "esri/geometry/Extent",
        "esri/layers/FeatureLayer"
    ],
            function (Query, ScreenPoint, Extent, FeatureLayer)
            {
                console.log ("zoomFloorOutline")
                clearGraphics ();
                var query = new Query ();
                query.objectIds = [id];
                var myDate = new Date ();
                var myMS = myDate.getMilliseconds ();
                query.where = "(" + myMS + "=" + myMS + ")";
                console.log ("zoomFloorOutline + id")

                floorOutline.selectFeatures (query, FeatureLayer.SELECTION_NEW, function (features)
                {
                    var floorExtent = features[0].geometry.getExtent ().expand (1.5);
                    map.setExtent (floorExtent);
                });
            });
}

 

How do i do something similar with JS 4.x?  I have some code to zoom to the feature but it is visible when the map is loaded and i just want show the quereied floor. 

function zoomFloorOutline (id)
{
    require ([
        "esri/rest/query",
        "esri/rest/support/Query",
        "esri/layers/FeatureLayer",
       "esri/views/layers/LayerView"
    ],
            function (query, Query, FeatureLayer,LayerView)
            {
                console.log ("zoomFloorOutline");

//                let highlight;
                view.whenLayerView (floorOutline).then (function (layerView)
                {

                    var query = floorOutline.createQuery ();
                    query.objectIds = [id];
                    query.where = "1=1";
                    floorOutline.queryFeatures (query).then (function (results)
                    {
                        const features = results.features;
                        console.log("zoom Floor" + features)
                        var geometry = features[0].geometry.extent.clone ().expand (2);
                        view.goTo (geometry);
                        
                        
                    });
                });
            });
}

 

Thanks for any help. 

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

On line 25 of your 4.x code you have this to zoom to the feature:

view.goTo (geometry);

 

If you want to make the layer visible after zooming, you can change that line to the following:

view.goTo(geometry).then(function() {
	floorOutline.visible = true;
});

View solution in original post

7 Replies
JoelBennett
MVP Regular Contributor

If you're just wanting to show a particular set of features at any given time, and know the objectID values of that set, then using definitionExpression would be one way to do so:

function hideLayerFeatures(featureLayer) {
	featureLayer.definitionExpression = "1=0";
}

function showLayerFeatures(featureLayer, ids) {
	if (Array.isArray(ids))
		featureLayer.definitionExpression = featureLayer.objectIdField + " IN(" + ids.join(",") + ")";
	else if (typeof ids == "number")
		featureLayer.definitionExpression = featureLayer.objectIdField + " = " + ids.toString();
}
0 Kudos
Mr_Kirkwood
Occasional Contributor

I already have some code to grab the OBJECTID. How do I get the layer to turn on to the queried OBJECTID when I zoom to the Feature? I have set the visibility for the whole layer to "false"

0 Kudos
JoelBennett
MVP Regular Contributor

On line 25 of your 4.x code you have this to zoom to the feature:

view.goTo (geometry);

 

If you want to make the layer visible after zooming, you can change that line to the following:

view.goTo(geometry).then(function() {
	floorOutline.visible = true;
});
Mr_Kirkwood
Occasional Contributor

That worked. Thank you so much for your help.  

0 Kudos
Mr_Kirkwood
Occasional Contributor

Thanks again. Here is my updated code snippet:

function zoomFloorOutline (id)
{
    require ([
        "esri/rest/query",
        "esri/rest/support/Query",
        "esri/layers/FeatureLayer",
        "esri/views/layers/LayerView"
    ],
            function (query, Query, FeatureLayer, LayerView)
            {
                console.log ("zoomFloorOutline");
                floorOutline.definitionExpression = floorOutline.objectIdField + " = " + id.toString ();
                console.log (floorOutline.definitionExpression)
                view.whenLayerView (floorOutline).then (function (layerView)
                {
                    var query = floorOutline.createQuery ();
                    query.objectIds = [id];
                    query.where = "1=1";
                    floorOutline.queryFeatures (query).then (function (results)
                    {
                        const features = results.features;
                        var geometry = features[0].geometry.extent.clone ().expand (2);
                        view.goTo (geometry).then (function ()
                        {
                            floorOutline.visible = true;
                        });
                    });
                });
            });
}

How would this work with an array of OBJECTIDs? My JS 3.x looks like this:

function zoomRoomUse (ids)
{
    require ([
        "esri/tasks/query",
        "esri/geometry/Extent",
        "esri/layers/FeatureLayer"
    ],
            function (Query, Extent, FeatureLayer)
            {
                console.log ("zoomRoomUse")
                var query = new Query ();
                query.objectIds = ids;
                var myDate = new Date ();
                var myMS = myDate.getMilliseconds ();
                query.where = "(" + myMS + "=" + myMS + ")";
                roomUseLayer.selectFeatures (query, FeatureLayer.SELECTION_NEW, function (features)
                {
                    var newExtent = new Extent ();
                    newExtent = features[0].geometry.getExtent ();

                    for (var k = 0; k < features.length; k++) {
                        newExtent = newExtent.union (features[k].geometry.getExtent ().expand (15));
                    }
                    labelRoom ();
                });
            });
}

I am having trouble updating it to 4.x 

0 Kudos
Mr_Kirkwood
Occasional Contributor
I tried something like this:

function zoomRoomUse (ids)
{
    require ([
        "esri/rest/query",
        "esri/rest/support/Query",
        "esri/layers/FeatureLayer",
        "esri/views/layers/LayerView"
    ],
            function (query, Query, FeatureLayer, LayerView)
            {
                console.log ("zoomFloorOutline");
                roomsUseLayer.definitionExpression = roomsUseLayer.objectIdField + " IN(" + ids.join(",") + ")";
                console.log (roomsUseLayer.definitionExpression)
                console.log(ids)
                view.whenLayerView (roomsUseLayer).then (function (layerView)
                {
                    var query = roomsUseLayer.createQuery ();
                    query.objectIds = [ids];
                    query.where = "1=1";
                    roomsUseLayer.queryFeatures (query).then (function (results)
                    {
                        const features = results.features;
                        var geometry = features[0].geometry.extent.clone ();
                        for (var k = 0; k < features.length; k++) {
                            geometry = geometry.union (features[k].geometry.extent.clone ());
                            view.goTo (geometry).then (function ()
                            {
                                roomsUseLayer.visible = true;
                            });
                        }
                    });
                });
            });
}
0 Kudos
JoelBennett
MVP Regular Contributor

On line 21, instead of:

query.where = "1=1";

 

I think you need this instead:

query.where = roomsUseLayer.definitionExpression;

 

Or better yet, you could remove line 21 altogether, because according to the documentation for createQuery, the "where" setting should already be set according to the definitionExpression.  Setting it to "1=1" would overwrite that and return all features in the layer.

0 Kudos