我已经可以点击创建地标了!我想要的是阻止用户创建具有相同lat和long值的另一个地标。这是我基于Google Earth Api的初始代码。
不知何故它似乎不起作用......我如何确保用户不会在同一个lat上创建新的地标?
我认为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);
}
});
答案 0 :(得分:1)
匿名函数的逻辑有点多余。让我解释。
首先,您指定在目标'GEGlobe'对象上侦听“点击”事件。
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 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);
}
});