Adding layers to the map from queryTask

3756
7
Jump to solution
12-22-2015 08:11 AM
FlorianCADOZ
Occasional Contributor

Hi everyone,

I've got data on my arcgis server and I want to query these data thanks to the REST API and then, show the result of the query on the map !

I don't know how to explain it correctly but I would like to do the same thing that is done there​ but with the Web App Builder (for developpers) and not only with the API JS.

For now, my query is ok and I got a result (an array of features with, in each feature, an object Attributes and an object Geometry) but then and don't understand how to show this result as a layer on my map...

    _query_TablePositionSuffix: function (geoWhereClause){
        var queryTask = new QueryTask(this.arcgisServerURL + this.config.table_PositionSuffix);
        var query = new Query();
        query.returnGeometry = true;
        query.outSpatialReference = {"wkid":102100};
        query.returnDistinctValues = false;
        query.outFields = ['*'];
        query.where = geoWhereClause;
        queryTask.execute(query, lang.hitch(this, "on_query_TablePositionSuffix"));
    },
    
    on_query_TablePositionSuffix: function(results) {
        console.log(results);
     },

I tried some things like that but it didn't worked...

      array.forEach(results.features,function(feature){
            var graphic = feature;
            graphic.setSymbol(this.symbolPosition);            
            this.map.graphics.add(graphic);
          
        });

Thank you by advance for your help !

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Florian,

  So why not add the layer as FeatureLayer in selection mode?

In selection mode, features are retrieved from the server only when they are selected. Features are available on the client only while they are selected. To work with selected features:

  1. Call the selectFeatures method.
  2. Listen for the onSelectionComplete event.
  3. Once onSelectionComplete fires, retrieve the selected features using the getSelectedFeatures method.

When editing feature layers in selection mode, you will need to add the map service associated with the feature service to the map as a dynamic map service. If you do not have the map service added as a dynamic map service then the changes will not be visible because once the edits are complete the feature is no longer selected.

Here is a sample:

Select features within a two minute drive time | ArcGIS API for JavaScript

View solution in original post

7 Replies
RobertScheitlin__GISP
MVP Emeritus

Florian,

  It is probably a scope issue.

Try:

     on_query_TablePositionSuffix: function(results) {
        array.forEach(results.features, lang.hitch(this, function(feature){
            var graphic = feature;
            graphic.setSymbol(this.symbolPosition);
            this.map.graphics.add(graphic); 
        }));
     },
FlorianCADOZ
Occasional Contributor

Oh yeah... I feel stupid...

But my real problem is that I would like to have a Layer and not graphic ! I tried to use graphics because I was a bit distraught but my problem is to add a layer shown on the map (and by extend, in the layer list and the legend...)

Is it possible with something like "this.map.addLayer(..." ?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Florian,

   You will want to see this thread then:

FeatureLayer from FeatureCollection

It explains how to create a new FeatureLayer from query/selection results.

FlorianCADOZ
Occasional Contributor

Hello Robert,

I'm sorry for the response time but I had to work on another project in the meantime ...

So to return to the display of layers (and not graphic) I looked at the link you sent me and the associated code but I'm not sure that answers my question ...

In fact, I am performing a query on a published service via ArcGIS Server and after show this service as Layer as if it were the original (with the original symbology) but filtered through a Where clause! The doc associated with my application is here but there is no examples for it so I don't really understand how to do this...

Thx in advance for the help !

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Florian,

  So why not add the layer as FeatureLayer in selection mode?

In selection mode, features are retrieved from the server only when they are selected. Features are available on the client only while they are selected. To work with selected features:

  1. Call the selectFeatures method.
  2. Listen for the onSelectionComplete event.
  3. Once onSelectionComplete fires, retrieve the selected features using the getSelectedFeatures method.

When editing feature layers in selection mode, you will need to add the map service associated with the feature service to the map as a dynamic map service. If you do not have the map service added as a dynamic map service then the changes will not be visible because once the edits are complete the feature is no longer selected.

Here is a sample:

Select features within a two minute drive time | ArcGIS API for JavaScript

FlorianCADOZ
Occasional Contributor

oh yeah, that sounds good ! I'll try it !

Thank you for that tips !

0 Kudos
FlorianCADOZ
Occasional Contributor

Ok I found something similar to your solution and perfect for my goal thanks to you !

This is the MODE_ONDEMAND with a .setDefinitionExpression !

Thank you very much !

0 Kudos