Question

I am dynamically adding kml files to google earth. For this, I have written javascript functions to add a kml and to remove a kml. These functions work fine for the first time for a kml. But if called again they do not respond. This happens for each kml that I try to add or remove. If I keep the page on browser for some time, then these functions again respond once and again become unresponsive.

function add(id, fileurl)
            {
                var link = ge.createLink(''); 
                var href= fileurl; 
                link.setHref(href); 
                var networkLink = ge.createNetworkLink("'" + id + "'"); 
                networkLink.set(link, true, true); 
                ge.getFeatures().appendChild(networkLink);
            }

            function remove(id)
            {   
                for(var i=0; i<ge.getFeatures().getChildNodes().getLength(); i++)
                {
                    if(ge.getFeatures().getChildNodes().item(i).getId() == id || ge.getFeatures().getChildNodes().item(i).getId() == "'" + id + "'")
                    {
                        id = ge.getFeatures().getChildNodes().item(i).getId();
                        ge.getFeatures().removeChild(ge.getElementById(id));
                        break;
                    }
                }
Was it helpful?

Solution

The issue is that you can't re-add a feature using an ID that you have already used until all references to it have been released. This is usually done by the internal garbage collector - but you can also force it by calling release() on the object you are deleting. This ...

Permanently deletes an object, allowing its ID to be reused. Attempting to access the object once it is released will result in an error.

Also when an object is created with the API the object does not have a base address. In this case, the object can be returned by passing only its ID to getElementById(). This can then be used to remove the feature.

e.g.

function remove(id) {
  ge.getElementById(id).release();
}

Really though I would look to avoid using IDs altogether and would simply keep a variable that points to the feature, then use that to remove. e.g.

function add(fileurl) {
  var link = ge.createLink('');  //no id
  link.setHref(fileurl); 
  var networkLink = ge.createNetworkLink(''); //no id
  networkLink.set(link, true, true); 
  ge.getFeatures().appendChild(networkLink);
  return networkLink;
}

var link1 = add("http://yoursite.com/file.kml");
var link2 = add("http://yoursite.com/file2.kml"); // etc...

// then to remove, simply...

link1.release();
link2.release();

OTHER TIPS

OK. So I figured out that if you remove an object from GE, and then try to add another object with the same id, GE complains and won't create the object - unless some time (approx. 30 seconds in my case) has passed. This time actually is required by JavaScript to garbage collect the object. Setting the object to null doesn't give immediate result but may help Garbage Collector. Also release() method offered by GE does not help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top