Retrieving JavaScript's eventObject when using Google Maps API listener

て烟熏妆下的殇ゞ 提交于 2020-01-14 04:25:23

问题


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


回答1:


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
  }
}


来源:https://stackoverflow.com/questions/11118386/retrieving-javascripts-eventobject-when-using-google-maps-api-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!