So I have two html buttons that each run a different function (both functions are below). Basically, you click one of the two buttons to add a Google Maps actionlistener to the map. I’ve successfully got that to work. The only problem is that I only want the actionlistener to be available one click. After that one click I want the user to have to click another button before the actionlistener “listens” again. I hope that makes sense.
function addLaunch() {
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
infowindow.open(map, marker);
});
};
function addFishing() {
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
fishinfowindow.open(map, marker);
});
};
So I just tried this:
function addLaunch(setBoolean) {
var clicked = new Boolean(false);
clicked.boolValue = setBoolean;
if (clicked = true) {
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
infowindow.open(map, marker);
clicked.boolValue = false;
});
}
else {
google.maps.event.clearListeners(map, "click");
}
};
and it didn’t work….. Please point me in the right direction…
(BTW, the button passed “true” to the ‘setBoolean’.
This works to disable all actionlisteners after the first click. But it doesn’t reset after the button is clicked again.
var temp = true;
function addLaunch() {
if (temp == true) {
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
infowindow.open(map, marker);
temp = false;
if (temp == false) {
google.maps.event.clearListeners(map, "click");
}
});
}
}
Use
google.maps.event.addListenerOnce()From the documentation: