135

How do I trigger the onclick event of a marker on a Google Maps from outside the map?

I use version 3 of the API. I've seen many tutorials for version 2, but can't find this for version 3.

I have a global array (named markers) containing all the marker of the map (google.maps.Marker). Now I want do do something like:

markers[i].click(); //I know it's not working, but you get the idea...

//Next line seems to be the way in v2, but what's the equivalent in v3?
GEvent.trigger(markers[i], 'click');

Thanks for your help and if you need more info, let me know!

AlexV
  • 22,658
  • 18
  • 85
  • 122

2 Answers2

345

I've found out the solution! Thanks to Firebug ;)

//"markers" is an array that I declared which contains all the marker of the map
//"i" is the index of the marker in the array that I want to trigger the OnClick event

//V2 version is:
GEvent.trigger(markers[i], 'click');

//V3 version is:
google.maps.event.trigger(markers[i], 'click');
AlexV
  • 22,658
  • 18
  • 85
  • 122
  • click is firing, but in my case info windows is not adjusting to the map, it cut down slightly. – Saboor Awan Sep 20 '11 at 07:31
  • 3
    @Saboor Awan Try to ask a specific question about this as comments are not the best way to sort this out. – AlexV Sep 20 '11 at 13:22
  • This is is still correct. Remember that jQuery also has a `trigger()` function. In my case intellisense was popping up causing me to transpose the arguments. – Pete Apr 23 '12 at 01:57
  • Saboor Awan, try calling this function after some time (miliseconds) after the UI has been set (using setTimeout) – George Mavritsakis Apr 26 '13 at 21:54
  • 3
    Doing this for v3 gives me `TypeError: a is undefined in main.js (line 16, col 894)` What would be the reason for that? – invot Jul 25 '14 at 19:54
  • 1
    @invot Can't really help without seeing code, but I found someone with the same problem while doing a quick search... Try to set the "popupMapIn" width and height in CSS using pixels (px) and not percents (%). – AlexV Oct 29 '14 at 14:15
  • That was what fixed it so long ago. Thanks for the reply! – invot Oct 30 '14 at 18:22
  • @AlexV where did you find that? – Legionar Mar 19 '15 at 15:21
  • @Legionar Found what? The solution or the CSS error? The solution was found by debugging with [Firebug](http://getfirebug.com/) and the CSS main.js error was simply found by doing a Google search that lead me to [this post](http://gmap3.net/forum/viewtopic.php?id=184). – AlexV Mar 19 '15 at 15:47
  • @AlexV I already found out, what was the problem, it was my mistake in jquery... I had the same error "a is undefined in main.js on line 16"... it was not CSS error, but mine `markers[i]` has undefined index `i`... – Legionar Mar 19 '15 at 19:25
  • There's a "trigger" event on GMaps V3, just like the one on JQuery: https://developers.google.com/maps/documentation/javascript/reference – Pedro Ferreira Oct 20 '16 at 12:33
  • @AlexV How can I call trigger at center of map screen. I want that whenever button is pressed , marker is placed at center of map. So how to do that – Ankesh kumar Jaisansaria Oct 23 '16 at 13:24
  • @AlexV how can i pass arguments any idea? – Umair Ahmed Nov 09 '16 at 07:18
  • @Niranjan N Raju See the [google.maps.Marker documentation](https://developers.google.com/maps/documentation/javascript/markers) on how to create markers – AlexV Jan 11 '18 at 15:48
  • 1
    This solution does not work in clustered markers (one of the group markers) situation. Please share suggestion if you have. Thanks. – Kamlesh Mar 06 '20 at 11:15
15

For future Googlers, If you get an error similar below after you trigger click for a polygon

"Uncaught TypeError: Cannot read property 'vertex' of undefined"

then try the code below

google.maps.event.trigger(polygon, "click", {});
Ergec
  • 11,608
  • 7
  • 52
  • 62