Select to view content in your preferred language

Select Feature by ID

1501
7
04-21-2011 08:39 AM
davidrenz
Deactivated User
i am trying to select a feature on a featureLayer by the IDNUM.  it looks like the selection is happening, but the selection is not showing up on the map. can anyone tell me what might be going on here?

thanks
david


       
function Init() {
            
            $.ajax(
                {
                    url: "<%=System.Web.HttpContext.Current.Request.ApplicationPath.ToString() %>/Projects/GetProjectDates",
                    cache: false,
                    success: function (data) {
                        loadConstructionDates(data.ProjectDataTable);
                    }
                }
             );

            var iniExtent = new esri.geometry.Extent({"xmin": 4249987, "ymin": 238755, "xmax": 4315098, "ymax": 274866, "spatialReference":{"wkid":2270}});
   
            map = new esri.Map("map", { extent:iniExtent,logo: false});
            map.enableScrollWheelZoom();

            var layer = new esri.layers.ArcGISTiledMapServiceLayer(BaseMap);
            map.addLayer(layer);

            var streets = new esri.layers.ArcGISTiledMapServiceLayer(TransportationMap);
            map.addLayer(streets);

            featureLayer = new esri.layers.FeatureLayer(FeatureMap, {
                outFields: ["*"],
                mode: esri.layers.FeatureLayer.MODE_SELECTION,
                id: "streetsFeatureLayer"
            });

            var fieldsSelectionSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 255, 255, 0.95]), 2);
            featureLayer.setSelectionSymbol(fieldsSelectionSymbol);
            
            map.addLayer(featureLayer);
            
            dojo.connect(featureLayer, "onSelectionComplete", selectionComplete);
 }
 
 function loadConstructionDates(constructionDates)
 {
     m_constructionProjectDates = constructionDates;
 
     if (m_constructionProjectDates.length > 0) {
      showConstructionProjects();
     }
 }
 
 
        function showConstructionProjects() {
            var streetIDs = "";
            
            for (p in m_constructionProjectDates) {
                var project = m_constructionProjectDates

;                 if (project.StreetIDNUM.length > 0) {                                         var id = project.StreetIDNUM;                     var query = new esri.tasks.Query();                     query.where = "IDNUM = " + id;                     featureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);                  }             }         }                function selectionComplete()         {             console.log("This one is done");         }

0 Kudos
7 Replies
davidrenz
Deactivated User
well, it never fails ... whenever i am at my wits end and leave a post here on the forum, the mere act of forming the question helps me to solve my problem.

so, i think i found a solution. i was not putting the graphics onto the map. the features were coming back, but i was not adding them to the graphics layer.

if anyone has a better way of doing this, i would love to know, but below is what i came up with.

thanks

function showConstructionProjects() {

   var fieldsSelectionSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color("#FF0000"), 4);

   for (p in m_constructionProjectDates) {
       var project = m_constructionProjectDates
;

       if (project.StreetIDNUM.length > 0) {
           
           var query = new esri.tasks.Query();
           query.where = "IDNUM = " + project.StreetIDNUM;

           featureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (features) {
               for (var i = 0; i < features.length; i++) {
                   var graphic = features;
                   graphic.setSymbol(fieldsSelectionSymbol);
                   streetGraphicsLayer.add(graphic);

                   console.log("Got a graphic");
               }
           });
       }

   }
}
0 Kudos
KellyHutchins
Esri Frequent Contributor
Since you are working with a feature layer you shouldn't have to re-add the graphics to the map. Try defining a selection symbol for the feature layer using featureLayer.setSelectionSymbol. Then when features are selected using selectFeatures they should automatically display on the map using the selection symbol you defined.
0 Kudos
davidrenz
Deactivated User
Kelly,

That does not seem to be happening for me, unless I add the graphics. Do i need to be using a FeatureServer service? Or can i use a MapServer service as well?

Either way, i am still having to add the graphics.

The way I am doing this is:

1). I get the IDNums that I want to show from the $ajax function
2). When those results are returned I call the function to show them
3). I loop through the results and create a query, then call selectFeatures

The only way those selected IDs are showing up is if i add them to a graphics layer.

What am i doing wrong?

thanks
david
0 Kudos
KellyHutchins
Esri Frequent Contributor
You can select from a map service based feature layer - you don't need a feature service.Can you post your code that creates the feature layer and sets the selection symbol.
0 Kudos
KellyHutchins
Esri Frequent Contributor
0 Kudos
davidrenz
Deactivated User
Kelly,

Am trying to get the code back to how it was before i made it add the graphics.
This is close to what it was.


function Init() {
   
 var iniExtent = new esri.geometry.Extent({"xmin": 4249987, "ymin": 238755, "xmax": 4315098, "ymax": 274866, "spatialReference":{"wkid":2270}});
 
   map = new esri.Map("map", { extent:iniExtent,logo: false});


   var layer = new esri.layers.ArcGISTiledMapServiceLayer(BaseMap);
   map.addLayer(layer);

   var streets = new esri.layers.ArcGISTiledMapServiceLayer(TransportationMap);
   map.addLayer(streets);

   dojo.connect(map, "onLoad", function() {
       
       $.ajax(
       {
           url: "<%=System.Web.HttpContext.Current.Request.ApplicationPath.ToString() %>/Projects/GetProjectDates",
           cache: false,
           success: function (data) {
               loadConstructionDates(data.ProjectDataTable);
           }
       });

   });
}



function loadConstructionDates(constructionDates)
{
    m_constructionProjectDates = constructionDates;

    if (m_constructionProjectDates.length > 0) {
        showConstructionProjects();
    }
}






function showConstructionProjects()
{
     
     var streetIds = "";
     for (p in m_constructionProjectDates) {
 
var project = m_constructionProjectDates
;
 if (streetIds.length < 1) {
  streetIds = project.StreetIDNUM;
 }
 else 
 {
  streetIds = streetIds + "," + project.StreetIDNUM;
 }     
    }
    var fieldsSelectionSymbol  = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,255,255,0.95]), 2);
    var service = '<%= System.Configuration.ConfigurationManager.AppSettings["StreetsFeatureService"].ToString() %>';
    
    featureLayer = new esri.layers.FeatureLayer(service, 
    {
        mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
        outFields: ["*"],
        infoTemplate: infoTemplate
     });
    
     dojo.connect(featureLayer,"onMouseOut",mouseOutHandler);
    
     featureLayer.setDefinitionExpression("IDNUM IN '" + streetIds + "'");
     featureLayer.setSelectionSymbol(fieldsSelectionSymbol);
     map.addLayer(featureLayer);
}



When I run that code, I get this error in Firebug:

Error: Unable to draw graphic (null): Unable to complete operation.
if(!dojo._hasResource["dijit._base.man...h","tr","xx","zh","zh-cn","zh-tw"]);



thanks
david
0 Kudos
davidrenz
Deactivated User
looks like a plain old QueryTask will work for what i need.
i probably don't need the FeatureLayer in this scenario.

thanks!
david
0 Kudos