Question

I have a project using Google maps api v3, and I am using google.maps.event.trigger to call or trigger the listener that is created when the map loads. In this case I set listener at polygon click, so when polygon is clicked, it opens the relevant infowindow and google.maps.event.trigger is set on another block of function in the same javascript file. It is running well when the whole web program is reloaded. But when I reload the map or map is re-initialized with the polygon with same variable object, the syntax google.maps.event.trigger does not run. This is my function that is using google.maps.event.trigger

 function callInfoPoly(nomor){
    alert(polygon[nomor].getPath().getAt(0));
    google.maps.event.trigger(polygon[nomor], "click");
    }

the alert runs well, it means the variable contains the polygon object, but google.maps.event.trigger doesn't run.

Was it helpful?

Solution

When you take a look into the console after triggering the click you'll see an error:

Uncaught TypeError: Cannot read property 'vertex' of undefined 

This undefined object is a PolyMouseEvent . This object will be passed to the click-callback when a real click occurs. But when you trigger the click programmatically this object is missing, what forces the error above(and somehow stops the binding of the click-listener after re-initializing the map ).

solution: pass an empty object as argument to the click-callback when you trigger the click programmatically:

google.maps.event.trigger(polygon[nomor], "click", {});

Demo: http://jsfiddle.net/doktormolle/jVFN2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top