Search code examples
google-earth-plugin

How to create a custom layer in google earth so I can set it's visibility


I am trying to render a whole heap of vectors in the google earth plugin. I use the parseKml method to create my Kml Feature object and store it in an array. The code looks something like below. I loop over a list of 10,000 kml objects that I return from a database and draw it in the plugin.

// 'currentKml' is a kml string returned from my DB. 
// I iterate over 10,000 of these
currentKmlObject = ge.parseKml(currentKml);
currentKmlObject.setStyleSelector(gex.dom.buildStyle({
    line: { width: 8, color: '7fff0000' }
}));
ge.getFeatures().appendChild(currentKmlObject);
// After this, I store teh currentKml object in an array so 
// I can manipulate the individual features.

This seems to work fine. But when I want to turn the visibility of all these features on or off at once, I have to iterate over all of these kml objects in my array and set their individual visibilities on or off. This is a bit slow. If I am zoomed out, I can slowly see each of the lines disappearing and it takes about 5 - 10 seconds for all of them to disappear or come back.

I was wondering if I could speed up this process by adding a layer and adding all my objects as children of this layer. This way I set the visibility of the whole layer on or off.

I have been unable to find out how to create a new layer in code though. If someone can point the appropriate methods, it would be great. I am not sure if a layer is the right approach to speed up the process either. If you also have any other suggestions on how I can speed up the process of turning on/off all these objects in the map at once, that would be very helpful as well.

Thanks in advance for you help.


Solution

  • Ok, found out how to do this by myself.

    In the google earth extensions libarary I use the 'buildFolder' method.

    var folder = gex.dom.buildFolder({ name: folderName });
    ge.getFeatures().appendChild(folder);
    

    Now, when I iterate over my object array, I add them to the folder instead using the following

    folder.getFeatures().appendChild(currentKmlObject);
    

    This way, later on I can turn the visibility on and off at the folder level using

    folder.setVisibility(false); // or true
    

    And this works quite well as well. IThere is no delay, I can see all the objects turning on and off at once. It is quite quick and performant.