Gal Appelbaum
Gal Appelbaum

Reputation: 1955

creating a Placemarks that can be hidden

I have been trying to create a Placemark that I can hide and show (like turning visibility on and off) on demand (on click)... I am using this to make the placemark:

function placemark(lat, long, name, url, iconsrc){
    var placemark = ge.createPlacemark(name);
    ge.getFeatures().appendChild(placemark);
    placemark.setName(name);

    // Create style map for placemark
    var icon = ge.createIcon('');
    if(iconsrc == "0")
        icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
    else{
        icon.setHref(iconsrc);
    }
    var style = ge.createStyle('');
    style.getIconStyle().setIcon(icon);
    if(iconsrc != "0")
        style.getIconStyle().setScale(2.5);

    placemark.setStyleSelector(style);

    // Create point
    var point = ge.createPoint('');
    point.setLatitude(lat);
    point.setLongitude(long);
    //point.setAltitudeMode(1500);
    placemark.setGeometry(point);
    google.earth.addEventListener(placemark, 'click', function(event) {
    // Prevent the default balloon from popping up.
        event.preventDefault();

        var balloon = ge.createHtmlStringBalloon('');
        balloon.setFeature(placemark); // optional
        balloon.setContentString(
            '<iframe src="'+ url +'" frameborder="0"></iframe>');

        ge.setBalloon(balloon);
    });              

}

I have tried everything... from this:

function hidePlacemark(name){
    var children = ge.getFeatures().getChildNodes();
    for(var i = 0; i < children.getLength(); i++) { 
        var child = children.item(i);
        if(child.getType() == 'KmlPlacemark') {
            if(child.getId()== name)
            child.setVisibility(false);
        }
    }
}

to using this ge.getFeatures().removeChild(child);

can anyone point me to the right direction on creating a function that will allow me to turn the visibility on/off on demand please.

Upvotes: 1

Views: 1571

Answers (2)

lifeIsGood
lifeIsGood

Reputation: 1229

Your hidePlacemark function is missing some {} in your final IF statement

if(child.getId()== name)

you have

function hidePlacemark(name){
    var children = ge.getFeatures().getChildNodes();
    for(var i = 0; i < children.getLength(); i++) { 
        var child = children.item(i);
        if(child.getType() == 'KmlPlacemark') {
            if(child.getId()== name)
            child.setVisibility(false);
        }
    }
}

make it

function hidePlacemark(name){
    var children = ge.getFeatures().getChildNodes();
    for(var i = 0; i < children.getLength(); i++) { 
        var child = children.item(i);
        if(child.getType() == 'KmlPlacemark') {
            if(child.getId()== name) {
                child.setVisibility(false);
            }
        }
    }
}

HOWEVER ------- you are better off doing this as it is much faster as you don't need to loop through ALL your placemarks

function hidePlacemark(name) {
    var placemark = ge.getElementById(name);
    placemark.setVisibility(false);
}

Upvotes: 2

Rafael Z. B. Bravo
Rafael Z. B. Bravo

Reputation: 1032

I think the plain ge.getFeatures().removeChild(placemark); works.

I played with this GooglePlayground, and just added the following code to line 8 (that is empty in this GooglePlayground Sample):

  addSampleButton('Hide Placemark', function(){ 
       ge.getFeatures().removeChild(placemark); 
  });

Clicking the button Hide Placemark hides the placemark like a charm here. Any chances your problem is somewhere else in your code?

Upvotes: 1

Related Questions