ChatGPT解决这个技术问题 Extra ChatGPT

How to trigger the onclick event of a marker on a Google Maps V3?

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!


A
AlexV

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');

click is firing, but in my case info windows is not adjusting to the map, it cut down slightly.
@Saboor Awan Try to ask a specific question about this as comments are not the best way to sort this out.
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 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 (%).
This solution does not work in clustered markers (one of the group markers) situation. Please share suggestion if you have. Thanks.
E
Ergec

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", {});

You saved my night. I was struggling for 3 hours on this! Thank you!!