Application crashes randomly when updating Kml Layer

2741
2
09-03-2015 12:45 PM
JasonTrinidad1
New Contributor III

I have a piece of code that updates a KML overlay often. Some times, at random, my application crashes after replacing the KML layer from my Scene. I do not get an error message, not does it let me debug it, so I suspect is somewhere in the ArcGis code. Is this the correct way to update a KML layer?

string id = objId + "Overlay";

System.IO.File.WriteAllText(_tempFilePath + @"\" + id + ".kml", kmlString);
var sourceUri = new Uri(_tempFilePath + @"\" + id + ".kml"); //I receive KML as a string, so I save as a file to get an URI

if (MySceneView.Scene.Layers[id] == null) //first time adding the layer
  {
     var kmlLayer = new KmlLayer(sourceUri);
     kmlLayer.ID = id;
     kmlLayer.Opacity = _alpha;
     MySceneView.Scene.Layers.Add(kmlLayer);
     return id;
  }
  else //Update the layer
  {
     for(int i = MySceneView.Scene.Layers.Count - 1; i >= 0; i--)//for each layer
     {
        if (MySceneView.Scene.Layers.ID == id) //find the matching ID
        {
           var kmlLayer = new KmlLayer(sourceUri);
           kmlLayer.ID = id;
           kmlLayer.Opacity = _alpha;
   
           MySceneView.Scene.Layers = kmlLayer; //repalce the layer with the updated one
           return id;
        }
     }
  }

return "";
0 Kudos
2 Replies
MichaelBranscomb
Esri Frequent Contributor

Hi,

What happens if you modify the code to remove the existing KMLLayer from the Scene and set it to null then insert the new KMLLayer at the original index?

Cheers

Mike

0 Kudos
JasonTrinidad1
New Contributor III

If I remove the existing layer, I can't then set it to null, since its removed from the collection. If I just remove the layer and add a new one, it also crashes randomly.

0 Kudos