1

我一直在尝试创建一个地标,我可以根据需要(点击时)隐藏和显示(例如打开和关闭可见性)......我正在使用它来制作地标:

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);
    });              

}

我已经尝试了一切......从这个:

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);
        }
    }
}

使用这个ge.getFeatures().removeChild(child);

谁能指出我创建一个功能的正确方向,该功能允许我按需打开/关闭可见性。

4

2 回答 2

2

您的 hidePlacemark 函数在您的最终 IF 语句中缺少一些 {}

if(child.getId()== name)

你有

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);
        }
    }
}

做了

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);
            }
        }
    }
}

但是-------你最好这样做,因为它更快,因为你不需要遍历所有地标

function hidePlacemark(name) {
    var placemark = ge.getElementById(name);
    placemark.setVisibility(false);
}
于 2013-04-19T02:38:47.613 回答
1

我认为平原的ge.getFeatures().removeChild(placemark);作品。

我玩过这个GooglePlayground,只是将以下代码添加到第 8 行(在这个GooglePlayground Sample中是空的):

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

单击按钮隐藏地标隐藏地标,就像这里的魅力一样。你的问题有没有可能在你的代码中的其他地方?

于 2013-04-18T23:26:36.280 回答