谷歌地球API,如何防止重复的标记
Google Earth API, how to prevent duplication of placemarks?
我已经可以在点击时创建占位符了!我想要的是防止用户创建另一个具有相同lat和long值的占位符。这是我基于GoogleEarthApi的初始代码。
不知怎么的,它似乎不起作用。。。如何确保用户不会在同一纬度上创建新的占位符?
我认为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);
}
});
匿名函数的逻辑有点多余。让我解释一下。
首先,您指定侦听目标"GEGlobe"对象上的"点击"事件。
google.earth.addEventListener(ge.getGlobe(), 'click', ...
然后,在条件语句中,您将测试事件的目标,即"GEGlobe"对象,是否不是KmlPlacemark或KmlPoint-但这始终是真的。这是因为事件传播的工作方式。事件总是会传播到GEGlobe,因此情况总是真实的。
if (event.getTarget().getType() != 'KmlPlacemark' &&
event.getTarget().getGeometry().getType() != 'KmlPoint') ...
您可以查看event.stopPropagation
和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);
}
});
相关文章:
- 防止Iframe窗体在新窗口中打开
- 如何更改bigquery API中的计费层选项
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 使用Facebook live API创建实时视频对象时的隐私设置
- 有没有一种方法可以防止img get请求使用css或js发生
- 谷歌地图API V3-防止ImageMapType包装
- 防止在地理编码器谷歌地图API中循环
- 防止以编程方式调用 django-rest-framework API
- 如何防止网站外部的 API 调用
- 网络音频API:防止麦克风输入通过扬声器播放
- 天气API,如何组合搜索参数以获得更详细的位置(城市、州、国家等),以防止位置混淆
- 谷歌地球API,如何防止重复的标记
- 谷歌api——防止标记在缩放时改变大小
- AngularJS / IonicFramework防止在toggle上调用多个API
- 防止地图刷新时,标记移动-谷歌地图API
- 防止多个chrome.storage API调用中出现竞争条件的最佳方法
- Brightcove智能播放器API与secureConnections参数混合用于防止安全警告
- 在谷歌地图 API V3 中,如何在不使用限制平移解决方案的情况下防止标记在 x 轴上重复
- 防止React Router路由服务器API路径