Chapter 3 Map Click Code Tweak
Published by Jeffrey November 27th, 2006 in Fixes, JavaScript, The BookUPDATE: A savvy reader Carl Olsen pointed out that it wasn’t actually necessary to check for the infoWindow state and the problem could simply be fixed by a check for an overlay. The post has been updated accordingly. Thanks Carl.
It seems that somewhere along the line something changed with the event handling and the Chapter 3 code wasn’t quite working right.
The problem listing is 3-4 where we defined the click event for the map. When you clicked inside the info window and associated form elements, or on a new marker, the click event for the map fired as well as the click event for the info window and overlays. According to the Google Maps API this is the desired effect, so the listing has been updated to include an if statement that ensures the click wasn’t on an overlay, and the info window isn’t already open:
if(!overlay) {
// run the code
}
This seems to have cleared up most of the issues with the only caveat being that you’ll have to manually close the infoWindow first to position it in a new location.
Thanks to those who pointed out the bug.


Get emailed automatically
Does it mean that I in this code on page 41 i should paste the new code:
GEvent.addListener(map, “click”, function(overlay, latlng) {
NEW CODE IN HERE SHOULD BE
———-
//only perform the click if the window is closed and the click was directly on the map.
if(!overlay) {
———-
//create an HTML DOM form element
var inputForm = document.createElement(”form”);
inputForm.setAttribute(”action”,”");
inputForm.onsubmit = function() {storeMarker(); return false;};