【问题标题】:Google Earth API, how to prevent duplication of placemarks?Google Earth API,如何防止地标重复?
【发布时间】:2012-02-12 07:44:48
【问题描述】:

我已经可以在点击时创建地标了!我想要的是防止用户创建另一个具有相同 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);

                    }
                  });

【问题讨论】:

标签: javascript google-earth-plugin


【解决方案1】:

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

首先,您指定监听 target 'GEGlobe' 对象上的 'click' 事件。

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

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

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

您可以查看 event.stopPropagation ala event.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);
  }  
});

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多