Feature Layer from Feature Collection applyEdits don't work

2769
20
Jump to solution
04-20-2017 03:45 AM
ZdeněkSoldán
Occasional Contributor

Hello,

In a widget I create a new Feature Layer from Feature Collection. From web service I want to add features to this Feature Layer. When I fire applyEdits() function to add the features everything goes well without any error message. But the Feature Layer is still empty. I tried to add edits-complete event and it also fired after complete adding features, so everything seems to be good but the Feature Layer is still empty.

My code is almost the same as this sample code Feature collection | ArcGIS API for JavaScript 3.20  

But I don't know where could be the problem because the whole code works. 

Have anyone an idea?

Thanks for any help.

0 Kudos
1 Solution

Accepted Solutions
ZdeněkSoldán
Occasional Contributor

Robert,

I found what was wrong. The problem was really in the response from web service. Our projection must have minus sign before number and x and y coords are swapped in web service. 

Now everything works. 

Thank you for your time.

View solution in original post

0 Kudos
20 Replies
RobertScheitlin__GISP
MVP Emeritus

Zdeněk,

   You would need to share your code. I do this all the time and have had FeatureLayer FeatureCollection working in my eSearch widget for a long time now.

0 Kudos
ZdeněkSoldán
Occasional Contributor

Robert,

Here is my code:

startup: function() {
        this.inherited(arguments);
        var map = this.map;
        var featureLayer;
        //create a feature collection 
        var featureCollection = {
           "layerDefinition": null,
           "featureSet": {
              "features": [],
              "geometryType": "esriGeometryPoint"
           }
        };
        featureCollection.layerDefinition = {
           "geometryType": "esriGeometryPoint",
           "objectIdField": "ObjectID",
           "drawingInfo": {
            "renderer": {
              "type": "simple",
              "symbol": {
                "type": "esriPMS",
                "url": "images/carElt.png",
                "contentType": "image/png",
                "width": 15,
                "height": 15
              }
            }
          },
           "fields": [{
              "name": "ObjectID",
              "alias": "ObjectID",
              "type": "esriFieldTypeOID"
           }, {
              "name": "car_spz",
              "alias": "Vozidlo (SPZ)",
              "type": "esriFieldTypeString"
           }, {
              "name": "Locality",
              "alias": "Lokalita",
              "type": "esriFieldTypeString"
           }, {
              "name": "Age",
              "alias": "Stáří",
              "type": "esriFieldTypeString"
           }]
        };
        //define a popup template
        var popupTemplate = new PopupTemplate({
           title: "{Vozidlo (SPZ)}",
           //description: "{description}"
        });
        //create a feature layer based on the feature collection
        featureLayer = new FeatureLayer(featureCollection, {
           id: 'carPosition',
           infoTemplate: popupTemplate
        });

        //associate the features with the popup on click
        featureLayer.on("click", function(evt) {
          map.infoWindow.setFeatures([evt.graphic]);
        });
        map.on("layers-add-result", function(results) {
          requestCar();
        });
        map.addLayers([featureLayer]);

        function requestCar() {
          var requestHandle = esriRequest({
            url: "http://server.domain.cz/ws/?cmd=getrealtimevehiclelist&auser=carposition&apass=0dotlE&outproj=JTSK&r...",
            handleAs: "json"
          });
          requestHandle.then(requestSucceeded, requestFailed);
        }
        function requestSucceeded(response, io) {
        //loop through the items and add to the feature layer
        var features = [];
        array.forEach(response.response.resultitems, function(item) {
          //console.log(item)
          var attr = {};
          attr["car_spz"] = item.fullname;
          attr["Locality"] = item.locality;
          attr["Age"] = item.age;
          //console.log(attr)
          var geometry = new Point({"x":item.position.x,"y":item.position.y,"spatialReference":{"wkid":5514}});
          //console.log(geometry)
          var graphic = new Graphic(geometry);
          graphic.setAttributes(attr);
          features.push(graphic);
        });
        console.log(features)
        featureLayer.applyEdits(features, null, null, function (add){
          array.forEach(add, function (a){
            console.log("success")
            console.log(a.status)
          })
        }, function (error){
          console.log("error")
          console.log(error)
        });
      }
      
      function requestFailed(error) {
        console.log('failed');
      } 
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

the features object is after forEach loop successfully filled. ApplyEdits goes through a function not error function but the feature layer remains empty.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Zdeněk,

   If you are using an esri basemap then you will need to re-project your graphics to 102100 as your 5514 data will not work properly since this is a featurecollection and does not automatically re-project your data.

ZdeněkSoldán
Occasional Contributor

Robert,

I am not using an esri basemap. My basemap has the same projection as created points.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

So if you where to add this code:

console.info(this.map.spatialReference.wkid);

it would return 5514? Can you check that for me?

0 Kudos
ZdeněkSoldán
Occasional Contributor

It returned 102067 which is the second wkid for the same projection. For sure I changed the wkid of points to the same 102067. But the feature layer is still empty. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

And your sure the data coming from 

http://server.domain.cz/ws/?cmd=getrealtimevehiclelist&auser=carposition&apass=0dotlE&outproj=JTSK&r...

is in WKID 102067 or 5514 and not something like 4326?

0 Kudos
ZdeněkSoldán
Occasional Contributor

Yes, I am sure. 

But maybe I found where is the problem. The feature layer I created has spatialReferece 4326. But in the documentation FeatureLayer | API Reference | ArcGIS API for JavaScript 3.20 I didn't find if it's possible to change spatialReference for feature layer made from featureCollection. I tried to add the code 

"spatialReference":[{
"wkid":102067
}],

to the layerDefiniton part but the spatialReference of the feature layer is still 4326.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Well the spatialReference is not expecting an array.

"spatialReference":{
"wkid":102067
},

But I don't think you can specify the spatialReference in the layerDefinition anyway.

I don't see how the layer is getting a wkid of 4326 since I don't see that in your code anywhere and the geometry you are adding has a spatialReference defined.

0 Kudos