0
google.maps.event.addListener(marker2, 'click', function() {
  infowindow2.open(map,marker2);
});

I need this function to be called on load of the page and not on click of marker2

I have seen Trigger click function on load however the syntax appears to be different. So i tried the following

I have tried several compinations, but can't seem to get the syntax right. could somebody please explain how to do this, and break down the code for myself and others to learn from

Thanks

Community
  • 1
  • 1
Henry Aspden
  • 1,863
  • 3
  • 23
  • 45
  • Did you refer to the [documentation for the Google Maps Javascript API v3](https://developers.google.com/maps/documentation/javascript/reference#event): `trigger(instance:Object, eventName:string, var_args:*) None Triggers the given event. All arguments after eventName are passed as arguments to the listeners.` – geocodezip Oct 17 '13 at 15:05

3 Answers3

1

If you want the infowindow to be open on page load, I think rather than triggering a click to open it - you'd be better off if you just called the open function right after creating your infowindow object.

var infowindow2 = new google.maps.InfoWindow(infoWindowOptions); //create your infowindow object with desired options
infowindow2.open(map, marker2); //call the open method right after creating your infowindow object

You can then add the listener for the click on the marker as well.

Suvi Vignarajah
  • 5,758
  • 27
  • 38
1

Your page loading just once, use addListenerOnce.

google.maps.event.addListenerOnce(map, 'idle', function() {
  infowindow2.open(map,marker2);
});
lontongcorp
  • 138
  • 1
  • 8
0

This should work if marker2 exists (has been created) before it is called:

google.maps.event.trigger("click", marker2);
geocodezip
  • 158,664
  • 13
  • 220
  • 245