1

我已经可以在点击时创建地标!我想要的是防止用户创建另一个具有相同 lat 和 long 值的地标。这是我基于 Google Earth Api 的初始代码。

不知何故,它似​​乎不起作用......我如何确保用户不会在同一个纬度上创建新的地标?

我想if (event.getTarget().getType() != 'KmlPlacemark' && event.getTarget().getGeometry().getType() != 'KmlPoint'应该做的伎俩..任何想法?T_T

google.earth.addEventListener(ge.getGlobe(), 'click', function(event) {
    if (event.getTarget().getType() != 'KmlPlacemark' &&
          event.getTarget().getGeometry().getType() != 'KmlPoint') {
                      event.preventDefault();
                      //create a place marker for the pole
                    var poleMarker = ge.createPlacemark('');
                    var point = ge.createPoint('');
                    point.setLatitude(event.getLatitude());
                    point.setLongitude(event.getLongitude());
                    poleMarker.setGeometry(point);

                    ge.getFeatures().appendChild(poleMarker);

                    }
                  });
4

1 回答 1

1

你的匿名函数的逻辑有点多余。让我解释。

首先,您指定侦听目标“GEGlobe”对象上的“点击”事件。

google.earth.addEventListener(ge.getGlobe(), 'click', ...

然后,在您的条件语句中,您正在测试事件的目标“GEGlobe”对象是否不是 KmlPlacemark 或 KmlPoint - 但这总是正确的。这是因为事件传播的工作方式。该事件将始终传播到 GEGlobe,因此条件将始终为真。

if (event.getTarget().getType() != 'KmlPlacemark' &&
          event.getTarget().getGeometry().getType() != 'KmlPoint') ...

您可以查看event.stopPropagationalaevent.preventDefault但对于您的情况,一个简单的解决方案“......以防止用户创建另一个具有相同 lat 和 long 值的地标......”将存储 lat lng 值,而不是创建地标,如果已经存储的值。例如,以下内容可能对您有用。显然还有其他方法可以做到这一点,但是存储位置然后检查它们的原则将成立,但你实际上编码它。

// to hold the places clicked
var locations = new Array();

google.earth.addEventListener(ge.getGlobe(), 'click', function(event)
{
  event.preventDefault();

  // create a string of the place
  var place = event.getLatitude() + ',' + event.getLongitude();

  // if the place is not the locations array
  if(locations.indexOf(place) == -1)
  {
    // add the place to the locations array
    locations.push(place);

    // create a place marker for the pole
    var poleMarker = ge.createPlacemark('');
    var point = ge.createPoint('');
    point.setLatitude(event.getLatitude());
    point.setLongitude(event.getLongitude());
    poleMarker.setGeometry(point);
    ge.getFeatures().appendChild(poleMarker);
  }  
});
于 2012-02-11T23:31:02.683 回答