我正在使用 Google Maps API 并尝试从触摸屏驱动 GE 插件。对于给定的触摸,我得到一个 X 和 Y,并且可以做类似的事情:
var targetElement = document.elementFromPoint(data.x, scaledY);
if (null != targetElement) {
var event = $.Event ("click");
$(targetElement).trigger (event);
}
为屏幕上的按钮引发事件,但这不会单击 GE 插件中的地标。有没有一种简单的方法可以在这些标记上触发点击事件?
非常感谢您提供的任何帮助。
最佳答案
据我所知,您无法为 Google Earth Api 中的任何对象手动引发任何事件,您只能为它们添加事件监听器作为命名或匿名方法。
但是,您当然可以使用自定义方法模拟地标的默认点击行为(打开气球、飞向等)。
我以前做过这个,它只要求每个功能在插件中都有一个唯一的 ID(通过 api 或在 kml 中设置)。然后,我将其用作根据其 ID 定位功能的方法。
在您的示例中,假设 targetEvement
也可以有相应的 ID 集,那么您可以使用此技术来模拟这样的“点击”。
var targetElement = document.elementFromPoint(data.x, scaledY);
if (null != targetElement) {
var event = $.Event ("click");
$(targetElement).trigger (event);
simulateClick(targetElement);
}
var simulateClick = function (element) {
// presuming 'ge' references the plugin
// we create a feature balloon based on the placemark
var id = element.attr('id');
var placemark = ge.getElementById(id); // corresponding placemark
var balloon = ge.createFeatureBalloon();
balloon.setFeature(placemark);
ge.setBalloon(balloon);
// Update the view in Google Earth to the placemark.
// if no abstract view is defined you could also use the placemarks
// latitude and longitude to construct a KmlCamera object.
ge.getView().setAbstractView(placemark.getAbstractView());
}
关于google-maps - 使用触摸屏 X 和 Y 单击地标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951584/