【问题标题】:Click placemarker using touchscreen X and Y使用触摸屏 X 和 Y 单击地标
【发布时间】:2012-12-19 11:36:33
【问题描述】:

我正在使用 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-maps google-maps-api-3 google-maps-markers google-earth-plugin google-maps-api-2


    【解决方案1】:

    AFAIK 您不能为 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());
    }
    

    【讨论】:

    • 如果你通过 fetchKml 加载你的 kml,那么你需要使用 var placemark = ge.getElementByUrl(url + '#' + id) 其中 url 显然是你的 kml 文件的 URL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多