Javascript 谷歌地球插件&x2014;如何检查ID为的元素是否已经存在?

Javascript 谷歌地球插件&x2014;如何检查ID为的元素是否已经存在?,javascript,google-earth,google-earth-plugin,Javascript,Google Earth,Google Earth Plugin,我通过以下方式创建和指定样式(例如C#): 如何检查GE中是否已经存在ID为style1的元素?如果我调用ge.createPlacemark(“placemark1”),第二次调用时会出现COM错误 我无法使用ge.getElementById(“style1”)获取元素-它总是返回null。有两件事,首先是一个访问器getComputedStyle,它允许您将对象样式属性作为KMLStyle对象获取 dynamic placemarkStyle = placemark1.getCompute

我通过以下方式创建和指定样式(例如C#):

如何检查GE中是否已经存在ID为
style1
的元素?如果我调用
ge.createPlacemark(“placemark1”)
,第二次调用时会出现
COM
错误


我无法使用
ge.getElementById(“style1”)
获取元素-它总是返回null。

有两件事,首先是一个访问器
getComputedStyle
,它允许您将对象样式属性作为KMLStyle对象获取

dynamic placemarkStyle = placemark1.getComputedStyle();
string id = placemarkStyle.getId();
也许您可以使用此方法根据需要引用样式对象

还可以通过将类型名称作为字符串传递给
getElementsByType
来获取特定类型的所有元素的数组。像这样的东西应该会起作用(尽管它未经测试…)


getElementById('style1')将检查元素是否已经存在于ge中。如果不存在,它将返回null。在创建具有相同id的多个placemark时可能会遇到该错误。在将样式添加到placemark之前,我想知道该样式是否存在:在以编程方式添加placemark时,我想获取特定样式(如果存在),或者创建新样式(如果不存在)。我尝试使用
ge.getElementById(“style1”)
ge.getElementsByType('style')
。第一个总是返回null,另一个则返回长度为零的容器。使用
var placemark1=ge.createPlacemark(“placemark1”)
(//ge.getFeatures().appendChild(placemark1);)
var placemark2=ge.getElementById(“placemark1”);//空
但我无法为功能添加样式。我不确定您在这里想说什么。如果调用
ge.getElementById(“placemark1”),代码中一定有bugplacemark1
的placemark后,code>为空-这根本没有意义。你能发布一个将编译的简化示例吗?您可能在代码中有语法错误,ID无效,或者您创建了多个具有相同ID的元素,或者您在尝试访问插件之前没有将该元素添加到插件中。
dynamic placemarkStyle = placemark1.getComputedStyle();
string id = placemarkStyle.getId();
public bool DoesStyleExist(string id)
{
  var styles = ge.getElementsByType('style');
  int l = styles.getLength();

  if(l == 0) 
  {
    // no styles
    return false;
  }

  for (var i = 0; i < l; ++i) {
    var style = style.item(i);
    var styleid = style.getId();
    if(id == styleid)
    {
      // matching style id
      return true;
    }
  }

  // no match
  return false;
}
  // create the placemark
  placemark = ge.createPlacemark('pm1');
  var point = ge.createPoint('');
  point.setLatitude(37);
  point.setLongitude(-122);
  placemark.setGeometry(point);

  // add the placemark to the earth DOM
  ge.getFeatures().appendChild(placemark);

  var styleMap = ge.createStyleMap('');

  // Create normal style for style map
  var normalStyle = ge.createStyle('style1');
  var normalIcon = ge.createIcon('icon1');
  normalIcon.setHref('http://maps.google.com/mapfiles/kml/shapes/triangle.png');
  normalStyle.getIconStyle().setIcon(normalIcon);

  // Create highlight style for style map
  var highlightStyle = ge.createStyle('style2');
  var highlightIcon = ge.createIcon('icon2');
  highlightIcon.setHref('http://maps.google.com/mapfiles/kml/shapes/square.png');
  highlightStyle.getIconStyle().setIcon(highlightIcon);

  styleMap.setNormalStyle(normalStyle);
  styleMap.setHighlightStyle(highlightStyle);

  // Apply stylemap to a placemark
  placemark.setStyleSelector(styleMap);

  alert(ge.getElementById('pm1')); // object 
  alert(ge.getElementById('style1')); // object 
  alert(ge.getElementById('style2')); // object

  DoesStyleExist('style1'); // true
  DoesStyleExist('style2'); // true
  DoesStyleExist('foo'); // false