Sunday, November 9, 2014

Google Map Javascript - A classic problem of closures

The intention was to have a few markers created in a loop and add info windows to it. Each info windows should have been popup when clicking on the marker. Each Info window was supposed to contain information about the marker clicked which is specific to that marker. Below was the initial code written which ended up in showing the marker only on the last marker created in the loop when clicked on whichever markers created.




But this did not work very well because of the closure problem. by the time click event happens, it is the last marker object get assisegned in the action and hence only the last marker object is getting that action. To avoid it, below had to be done. 


References: 
http://stackoverflow.com/questions/3158598/google-maps-api-v3-adding-an-infowindow-to-each-marker

No comments:

Post a Comment