Problems Adding Graphics to a Feature Layer using applyEdits

3051
1
09-21-2010 09:15 AM
MichaelRollins
New Contributor
I'm attempting to add a Graphic to a FeatureLayer.  Whenever I do, the graphic gets added, but then a Type Error pops up.  Neither the success callback nor the error callback are being triggered.  The error is propagating up to a higher calling function which checks for intersections.

If I add the Graphic through the .add function (instead of applyEdits) it will add fine, but then it won't assign an object ID.

The error reads: "Type Error: Cannot read property "name" of null".  There is no name property for the graphic.

The function that checks for intersection (where the error is bubbling up to):

[PHP]
intersectByGeometryService: function(result){
//      Ext.Msg.alert('Added a graphic to the layer');
       
        var resultGeometry = result.geometry;
        var resultOID = result.attributes.valueOf('Object ID');
        var layer = result.getLayer();
       
        // Loop through the graphics of the layer, snagging the geometries of any that are there into an array.
        var geometries = new Array();
        for(var i = 0; i < layer.graphics.length; i++){
            if(resultOID != layer.graphics.attributes.valueOf('Object ID')){
                geometries.push(layer.graphics.geometry);
            }
        }
//        var geometries = dojo.map(layer.graphics, function(feature){
//            if(resultOID != feature.attributes.valueOf('Object ID')){
//                return feature.geometry;
//            }
//        });
       
        this.geometryService.intersect(geometries, resultGeometry, this.highlightIntersections,
        function(err){
            Ext.Msg.alert('Error occurred when checking for intersection: ' + err);  // error propagates to here
        });
    }
[/PHP]

The function where I add the Graphics:

[PHP]
highlightIntersections: function(geometries){
        // When this function is called from intersection calculations, this refers to Window.
        var emp = Ext.getCmp('editormappanel');
        for(var i = 0; i < geometries.length; i++){
//            var symbol = new esri.symbol.SimpleFillSymbol({
//                type : "esriSFS",
//                style : "esriSFSSolid",
//                color : [255, 0, 0, 255],
//                outline : {
//                    type : "esriSLS",
//                    style : "esriSLSSolid",
//                    color : [255, 194, 0, 255],
//                    width : 2
//                }
//            });
            var attributes = {name: 'composite value', type: 0, description: 'Intersection'};
            var graphic = new esri.Graphic(geometries, null, attributes);
            emp.compositelayer.applyEdits([graphic], null, null, function(adds, updates, deletes){
                Ext.Msg.alert('Composite layer update successful');
            }, function(err){
                Ext.Msg.alert('Composite layer update failed: ' + err);
            });
            //this.map.graphics.add(new esri.Graphic(geometries, symbol));
        }
       
       
    }
[/PHP]
0 Kudos
1 Reply
MichaelRollins
New Contributor
Oy vey, silly mistake.

Originally, I was using

[PHP]this.map.graphics.add(new esri.Graphic(geometries, symbol)); [/PHP]

to add the graphics inside a loop.  When I switched over to using applyEdits, I neglected to figure on the loop that I was in.  When you call applyEdits, you pass in a callback as a param, breaking out of the loop part way through it.  This caused the properties of the new feature to not get created correctly.

Here is the updated code:

[PHP]
highlightIntersections: function(geometries){
        // When this function is called from intersection calculations, this refers to Window.
        var emp = Ext.getCmp('editormappanel');
       
        // Collect the new graphics to be pushed onto the map
        var newGraphics = new Array();
       
        for(var i = 0; i < geometries.length; i++){
           
            // Check if the geometry given is valid (geometries.rings.length > 0)
            if(geometries.rings.length > 0){
                var attributes = {type: 0, description: 'Intersection'};
                var graphic = new esri.Graphic(geometries, null, attributes);
                newGraphics.push(graphic);
            }
        }
       
        // Make sure there is something to push onto the map.  If there is not, don't
        // make the call to applyEdits.
        if(newGraphics.length > 0){
            emp.compositelayer.applyEdits(newGraphics, [], [], function(adds, updates, deletes){
                Ext.Msg.alert('Composite layer update successful');
            }, function(err){
                Ext.Msg.alert('Composite layer update failed: ' + err);
            });
        }
    }
[/PHP]

The problem with the name property of null persists, though it doesn't block the addition of the features.  I suspect that it may have to do with the callbacks stacking and the blocking of the alerts.
0 Kudos