Search code examples
google-earth

Check/uncheck element through link inside Google Earth


So I have a folder with 2 placemarks in it. One of them is hidden on load while the other is visible. I'd like to have a link in the description of the visible one to hide/show the hidden one. Is there a way to do this?


Solution

  • If you are using the Google Earth plugin, this is one way of doing it.

    First assign each placemark an 'id' - so in the KML file you need to edit the code that says

       <Placemark>
       <name>Placemark 1</name>
    

    to

       <Placemark id="placemark1">
       <name>Placemark 1</name>
    

    and the same for Placemark #2

    then you have a javascript function like the one below, which is executed by adding a button through the balloon description (see this example page for how to do that)

        function togglePlacemarks() {
             var pm1 = ge.getElementById('placemark1');
             var pm1 = ge.getElementById('placemark1');
             if (pm1.getVisibility() == true) {
        pm1.setVisibility(false);
                pm2.setVisibility(true);
         } else {
        pm1.setVisibility(true);
                pm2.setVisibility(false);
         }
        }
    

    In the example code to execute JS from a balloon, you would change the following line

         balloon.setContentString(
        '<a href="#" onclick="alert(\'Running some JavaScript!\');">Alert!</a>');
    

    to

          balloon.setContentString(
        '<a href="#" onclick="togglePlacemarks();">Toggle Visibility</a>');
    

    Finally, if you only wish to have a simple (ie only 2 placemarks), then perhaps you should use the example code to create your placemarks instead of creating and loading a custom KML file. In which case, when you create the placemarks (through this line of code)

         placemark = ge.createPlacemark('');
    

    You would do this

         placemark1 = ge.createPlacemark('placemark1');
         ...etc
    

    and also

         placemark2 = ge.createPlacemark('placemark2');
         ... etc
         placemark2.setVisibility(false);