2

I can't understand how to display all features from the feature layer. In the code bellow I specified 2 features in the layer and set in feature layer's mode to esri.layers.FeatureLayer.MODE_SNAPSHOT. According esri documentation this options means that all features will be selected and displayed. But they are not. The code is:

   var jsonFS = {
                "displayFieldName": "Name",
                "fieldAliases": {
                    "Name": "Name"
                },
                "geometryType": "esriGeometryPoint",
                "spatialReference": {
                    "wkid": 102100 //WGS_1984_Web_Mercator_Auxiliary_Sphere
                },
                "fields": [{
                    "name": "Name",
                    "type": "esriFieldTypeString",
                    "alias": "Name",
                    "length": 255
                }],
                "features": [{
                    "attributes": {
                        "Name": "bla"
                    },
                    "geometry": {
                        "x": -8919439.31450887,
                        "y": 4928270.761925456
                    }
                },
                {
                    "attributes": {
                        "Name": "bla bla"
                    },
                    "geometry": {
                        "x": -8155495.379532158,
                        "y": 5075380.311392084
                    }
                }]
            };
            var fs = new esri.tasks.FeatureSet(jsonFS);

            var featureCollection = {
                layerDefinition: {
                  "geometryType": "esriGeometryPoint",
                  "fields": [
                  {
                    "name": "Name",
                    "type": "esriFieldTypeString",
                    "alias": "Name"
                  }
                  ]
                },
                featureSet: fs
            };

        var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
                mode: esri.layers.FeatureLayer.MODE_SNAPSHOT
            });
1
  • Updated my answer. In my testing, the issue seemed to be the lack of an ObjectID field. If you're familiar with Esri stuff, and understand how selections work in ArcGIS, this might make sense as an ObjectID is required for selections but I've never seen this mentioned in the feature layer documentation... Commented Apr 25, 2011 at 16:41

1 Answer 1

3

It looks like your spatial reference doesn't match your coordinates. Wkid 4326 == WGS84 which is lat/long but your coordinates are definitely not lat/long. I haven't tested your code but if fixing the spat ref issue doesn't work I'll start testing some stuff.

Edit: I played around with the code you posted and it looks like you need to specify an ObjectID field. I was tipped off to this when I loaded your code and saw this in the console:

esri.layers.FeatureLayer: objectIdField is not set [url: ]

It's also worth noting that even without an OID field, I did see one point displayed near Pittsburgh. So...here's a working example (you'll probably want to define an additional field rather than modifying "Name" to be your OID field):

<!doctype html>
<html>
  <head>
    <title>...</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.2/js/dojo/dijit/themes/claro/claro.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.2"></script>
    <script type="text/javascript" language="Javascript">
      dojo.require("esri.map");
      dojo.require("esri.layers.FeatureLayer");      
      var map;      
      function init() {
        var startExtent = new esri.geometry.Extent({"xmin":-12505511,"ymin":2230016,"xmax":-3699965,"ymax":8100380,"spatialReference":{"wkid":102100}});
        map = new esri.Map("mapDiv",{ extent: startExtent });
        //create and add new layer
        var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        map.addLayer(layer);

        var jsonFS = {
          "displayFieldName": "Name",
          "fieldAliases": {
              "Name": "Name"
          },
          "geometryType": "esriGeometryPoint",
          "spatialReference": {
              "wkid": 102100 //WGS_1984_Web_Mercator_Auxiliary_Sphere
          },
          "fields": [{
              "name": "Name",
              "type": "esriFieldTypeOID",
              "alias": "Name"
          }],
          "features": [{
              "attributes": {
                  "Name": "1"
              },
              "geometry": {
                  "x": -8919439.31450887,
                  "y": 4928270.761925456
              }
          },
          {
              "attributes": {
                  "Name": "2"
              },
              "geometry": {
                  "x": -8155495.379532158,
                  "y": 5075380.311392084
              }
          }]
        };
        console.log(jsonFS.features);
        var fs = new esri.tasks.FeatureSet(jsonFS);

        var featureCollection = {
          layerDefinition: {
            "geometryType": "esriGeometryPoint",
            "fields": [
            {
              "name": "Name",
              "type": "esriFieldTypeOID",
              "alias": "Name"
            }
            ]
          },
          featureSet: fs
        };

        var jsonfl = new esri.layers.FeatureLayer(featureCollection, {
          mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
          'id': 'jsonfl'
        });
        map.addLayer(jsonfl);
      }
      dojo.ready(init);
    </script>
  </head>
  <body class="claro">
    <div id="mapDiv" style="width:900px; height:600px; border:1px solid #000;"></div>
  </body>
</html>
3
  • It's my fault - in real code, I used lat/long coordinates and then convert them in the map's format using esri.geometry.geographicToWebMercator. Commented Apr 25, 2011 at 14:26
  • i fixed the code and specified apropriate spacial ref Commented Apr 25, 2011 at 14:38
  • OK, cool. I have to take care of a few things but should be able to play with this a bit more later today. Commented Apr 25, 2011 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.