Dan
Dan

Reputation: 872

Retrieving JavaScript's eventObject when using Google Maps API listener

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

Upvotes: 1

Views: 730

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

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

Upvotes: 1

Related Questions