featureLayer.setSelectionSymbol(symbol) not working

2571
5
07-27-2016 10:10 AM
lasinh
by
New Contributor III

i created featureLayer based on a feature collection; and i added this featureLayer  to my map; then i query features by Extent and objectId and my code return list features but not set symbol of features? how to set symbol of features returned from the query feature collection featurelayer;

below mycode:

require(["esri/map",

    "esri/layers/ArcGISTiledMapServiceLayer",

    "esri/dijit/PopupTemplate",

    "esri/layers/FeatureLayer",

    "esri/tasks/query",

    "esri/geometry/Circle",

    "esri/graphic",

    "esri/InfoTemplate",

    "esri/symbols/SimpleMarkerSymbol",

    "esri/symbols/SimpleLineSymbol",

    "esri/symbols/SimpleFillSymbol",

    "esri/Color",

    "esri/renderers/SimpleRenderer",

    "esri/geometry/Point",

    "dojo/_base/array",

    "dojo/on",

    "dojo/dom",

    "esri/layers/ArcGISDynamicMapServiceLayer",

    "dojo/domReady!"],

    function (Map,

        ArcGISTiledMapServiceLayer,

        PopupTemplate,

        FeatureLayer,

        Query,

        Circle,

        Graphic,

        InfoTemplate,

        SimpleMarkerSymbol,

        SimpleLineSymbol,

        SimpleFillSymbol,

        Color,

        SimpleRenderer,

        Point,

        arrayUtils,

        on,

        dom,

        ArcGISDynamicMapServiceLayer) {

        var map;

        var featureLayer;

        map = new Map("mapDiv", {

        });

        var tiled = new ArcGISTiledMapServiceLayer("url");    

        map.addLayers([tiled]);

        var symbol = new SimpleMarkerSymbol(

          SimpleMarkerSymbol.STYLE_CIRCLE,

          12,

          new SimpleLineSymbol(

            SimpleLineSymbol.STYLE_NULL,

            new Color([247, 34, 101, 0.9]),

            1

          ),

          new Color([207, 34, 171, 0.5])

        );

        var circle;

        var circleSymb = new SimpleFillSymbol(

          SimpleFillSymbol.STYLE_NULL,

          new SimpleLineSymbol(

            SimpleLineSymbol.STYLE_SHORTDASHDOTDOT,

            new Color([105, 105, 105]),

            2

          ), new Color([255, 255, 0, 0.25])

        );

        var fields = [

         {

             name: "ObjectID",

             alias: "ObjectID",

             type: "esriFieldTypeOID"

         }, {

             name: "matrai",

             alias: "matrai",

             type: "esriFieldTypeString"

         }

         , {

             name: "tentrai",

             alias: "tentrai",

             type: "esriFieldTypeString"

         }

         , {

             name: "loaiTrangTrai",

             alias: "loaiTrangTrai",

             type: "esriFieldTypeString"

         }

        ];

        var pTemplate = new PopupTemplate({

            title: "{tentrai}",

            description: "{loaiTrangTrai}"

        });

        map.on("layers-add-result", function () {

            $.ajax({

                type: "POST",

                url: "trangchu.aspx/GetPointJsonString",

                contentType: "application/json; charset=utf-8",

                dataType: "json", // dataType is json format

                success: function (data) {

                    var obj = JSON.parse(data.d);

                    var features = [];

                    $.each(obj, function (index, val) {

                        index = index + 1;

                        var feature = {

                            geometry: new Point({

                                x: val.toadox,

                                y: val.toadoy,

                                spatialReference: map.spatialReference

                            }),

                            // select only the attributes you care about

                            attributes: {

                                ObjectID: index,

                                matrai: val.matrai,

                                tentrai: val.tentrai,

                                loaiTrangTrai: val.loaiTrangTrai

                            }

                        };

                        features.push(feature);

                    });

                    var featureCollection = {

                        "layerDefinition": {

                            "geometryType": "esriGeometryPoint",

                            "objectIdField": "ObjectID",

                            "drawingInfo": {

                                "renderer": {

                                    "type": "simple",

                                    "symbol": {

                                        "type": "esriPMS",

                                        "url": "Images/GreenPin1LargeB.png",

                                        "contentType": "image/png",

                                        "width": 12,

                                        "height": 12

                                    }

                                }

                            },

                            "fields": fields

                        },

                        "featureSet": {

                            "features": features,

                            "geometryType": "esriGeometryPoint"

                        }

                    };

                    featureLayer = new FeatureLayer(featureCollection, {

                        id: 'trangtraiheo',

                        allowGeometryUpdates: true,

                        infoTemplate: pTemplate

                    });

                    map.addLayers([featureLayer]);

                    featureLayer.on("Click", function (evt) {

                        //    map.infoWindow.setFeatures([evt.graphic]);

                    });         

      },

                error: function (errorText) {

                    alert(errorText);

                }

            });

        });

        map.on("Click", function (evt) {        

            circle = new Circle({

                center: evt.mapPoint,

                radius: 1,

                radiusUnit: "esriMiles"

            });

            map.graphics.clear();

            map.infoWindow.hide();

            var graphic = new Graphic(circle, circleSymb);

            map.graphics.add(graphic);

            var query = new Query();

            query.geometry = circle.getExtent();

            featureLayer.queryFeatures(query, selectInBuffer);

        });

        function selectInBuffer(response) {

            var feature;

            var features

            features = response.features;

            var inBuffer = [];

            for (var i = 0; i < features.length; i++) {

                feature = features;

                if (circle.contains(feature.geometry)) {

                    inBuffer.push(feature.attributes[featureLayer.objectIdField]);

                }

            }

            var query = new Query();

            query.objectIds = inBuffer;

            featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (results) {

                featureLayer.setSelectionSymbol(symbol);

              

            });

        }

});

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

La Sinh,

  Have you tried setting the selection symbol outside/before your selectFeatures function?

0 Kudos
lasinh
by
New Contributor III

thanks Robert Scheitlin  i tried  setSelectionSymbol  before selectFeatures function but my code not work?

require(["esri/map",

    "esri/layers/ArcGISTiledMapServiceLayer",

    "esri/dijit/PopupTemplate",

    "esri/layers/FeatureLayer",

    "esri/tasks/query",

    "esri/geometry/Circle",

    "esri/graphic",

    "esri/InfoTemplate",

    "esri/symbols/SimpleMarkerSymbol",

    "esri/symbols/SimpleLineSymbol",

    "esri/symbols/SimpleFillSymbol",

    "esri/Color",

    "esri/renderers/SimpleRenderer",

    "esri/geometry/Point",

    "dojo/_base/array",

    "dojo/on",

    "dojo/dom",

    "esri/layers/ArcGISDynamicMapServiceLayer",

    "dojo/domReady!"],

    function (Map,

        ArcGISTiledMapServiceLayer,

        PopupTemplate,

        FeatureLayer,

        Query,

        Circle,

        Graphic,

        InfoTemplate,

        SimpleMarkerSymbol,

        SimpleLineSymbol,

        SimpleFillSymbol,

        Color,

        SimpleRenderer,

        Point,

        arrayUtils,

        on,

        dom,

        ArcGISDynamicMapServiceLayer) {

        var map;

        var featureLayer;

        map = new Map("mapDiv", {

        });

        var tiled = new ArcGISTiledMapServiceLayer("url");    

        map.addLayers([tiled]);

        var symbol = new SimpleMarkerSymbol(

          SimpleMarkerSymbol.STYLE_CIRCLE,

          12,

          new SimpleLineSymbol(

            SimpleLineSymbol.STYLE_NULL,

            new Color([247, 34, 101, 0.9]),

            1

          ),

          new Color([207, 34, 171, 0.5])

        );

        var circle;

        var circleSymb = new SimpleFillSymbol(

          SimpleFillSymbol.STYLE_NULL,

          new SimpleLineSymbol(

            SimpleLineSymbol.STYLE_SHORTDASHDOTDOT,

            new Color([105, 105, 105]),

            2

          ), new Color([255, 255, 0, 0.25])

        );

        var fields = [

         {

             name: "ObjectID",

             alias: "ObjectID",

             type: "esriFieldTypeOID"

         }, {

             name: "matrai",

             alias: "matrai",

             type: "esriFieldTypeString"

         }

         , {

             name: "tentrai",

             alias: "tentrai",

             type: "esriFieldTypeString"

         }

         , {

             name: "loaiTrangTrai",

             alias: "loaiTrangTrai",

             type: "esriFieldTypeString"

         }

        ];

        var pTemplate = new PopupTemplate({

            title: "{tentrai}",

            description: "{loaiTrangTrai}"

        });

        map.on("layers-add-result", function () {

            $.ajax({

                type: "POST",

                url: "trangchu.aspx/GetPointJsonString",

                contentType: "application/json; charset=utf-8",

                dataType: "json", // dataType is json format

                success: function (data) {

                    var obj = JSON.parse(data.d);

                    var features = [];

                    $.each(obj, function (index, val) {

                        index = index + 1;

                        var feature = {

                            geometry: new Point({

                                x: val.toadox,

                                y: val.toadoy,

                                spatialReference: map.spatialReference

                            }),

                            // select only the attributes you care about

                            attributes: {

                                ObjectID: index,

                                matrai: val.matrai,

                                tentrai: val.tentrai,

                                loaiTrangTrai: val.loaiTrangTrai

                            }

                        };

                        features.push(feature);

                    });

                    var featureCollection = {

                        "layerDefinition": {

                            "geometryType": "esriGeometryPoint",

                            "objectIdField": "ObjectID",

                            "drawingInfo": {

                                "renderer": {

                                    "type": "simple",

                                    "symbol": {

                                        "type": "esriPMS",

                                        "url": "Images/GreenPin1LargeB.png",

                                        "contentType": "image/png",

                                        "width": 12,

                                        "height": 12

                                    }

                                }

                            },

                            "fields": fields

                        },

                        "featureSet": {

                            "features": features,

                            "geometryType": "esriGeometryPoint"

                        }

                    };

                    featureLayer = new FeatureLayer(featureCollection, {

                        id: 'trangtraiheo',

                        allowGeometryUpdates: true,

                        infoTemplate: pTemplate

                    });

     
//  featureLayer.setSelectionSymbol(symbol);

                    map.addLayers([featureLayer]);

//  featureLayer.setSelectionSymbol(symbol);

                    featureLayer.on("Click", function (evt) {

                        //    map.infoWindow.setFeatures([evt.graphic]);

                    });         

      },

                error: function (errorText) {

                    alert(errorText);

                }

            });

        });

        map.on("Click", function (evt) {        

            circle = new Circle({

                center: evt.mapPoint,

                radius: 1,

                radiusUnit: "esriMiles"

            });

            map.graphics.clear();

            map.infoWindow.hide();

            var graphic = new Graphic(circle, circleSymb);

            map.graphics.add(graphic);

            var query = new Query();

            query.geometry = circle.getExtent();

            featureLayer.queryFeatures(query, selectInBuffer);

        });

        function selectInBuffer(response) {

            var feature;

            var features

            features = response.features;

            var inBuffer = [];

            for (var i = 0; i < features.length; i++) {

                feature = features;

                if (circle.contains(feature.geometry)) {

                    inBuffer.push(feature.attributes[featureLayer.objectIdField]);

                }

            }

  featureLayer.setSelectionSymbol(symbol);

            var query = new Query();

            query.objectIds = inBuffer;

            featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (results) {             

            });

        }

});

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

la sinh,

Are you sure that you have features being selected? Have you tried to put a console statement in the selectFeatures function?

0 Kudos
lasinh
by
New Contributor III

i get features atrrbutes and put this features console, but not set symbol this feature selected

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

La Sinh,

  SO you are saying that you do get something in the console when you have this code:

featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (results) {

   console.info(results);

});

0 Kudos