使用Google Maps API侦听器时检索JavaScript的eventObject [英] Retrieving JavaScript's eventObject when using Google Maps API listener

查看:132
本文介绍了使用Google Maps API侦听器时检索JavaScript的eventObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌地图的API V3中,我想添加一个标记到地图,如果用户控制点击地图。
为此,我添加了一个监听器到地图中,如下所示:

  google.maps.event。如果(event.ctrlKey)
add_marker(e.position);
});

侦听器传递的 e 参数包含一些数据,但主要是关于点击的位置,而我希望能够问在用户点击地图时,是否按下了控制按钮。



我发现chrome有一个对象 event ,这是默认的Javascript eventObject,它包含我需要的数据(ctrlKey),这确实在chrome中有效。



然而,当我在FF中尝试相同的代码时,找不到一个名为event的对象,我找不到一个方法来检索它。



DanC

$ b

感谢您的帮助, =h2_lin>解决方案

API没有提及任何有关访问DOM事件对象的信息。



传递给回调函数当前包含一个属性 b ,它是指t他的事件对象,所以你可以使用 ebctrlKey



没有记录,这是不可靠的,明天可能会改变。



另一个选项:

您可以观察事件的div包含不使用API​​方法的地图:

  map.getDiv()。onclick = function(e)
{
e = window.event || e;
if(e.ctrlKey)
{
//做某事
}
}


In google maps API V3, I would like to add a marker to the map if the user control-clicked the map. For that, I've added a listener to the map, as follows -

google.maps.event.addListener(map, 'click', function(e){
if (event.ctrlKey)
    add_marker(e.position);
});

The e parameter, passed by the listener, contains some data, but mostly regarding the position of the click, while I want to be able to ask if the control button was pressed during the time the usser clicked on the map.

I found that chrome had an object event, which is the default Javascript's eventObject, that contained the data I needed (ctrlKey) and this indeed works in chrome.

However, when I tried the same code in FF, it couldn't find an object called 'event', and I can't find a way to retrieve it.

I would appreciate your help in finding a solution that will work on IE too.

Thank, DanC

解决方案

The API doesn't say anything about accessing the DOM-event-object.

The argument passed to the callback-function currently contains a property b which refers to the event-object, so you could use e.b.ctrlKey

But as this is not documented it's not reliable may change tomorrow.

Another option:
You may observe the event for the div that contains the map without using the API-method:

map.getDiv().onclick=function(e)
{
  e=window.event||e;
  if (e.ctrlKey)
  {
    //do something
  }
}

这篇关于使用Google Maps API侦听器时检索JavaScript的eventObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆