Reputation: 349
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");
}
});
}
}
Upvotes: 2
Views: 658
Reputation: 9407
Use google.maps.event.addListenerOnce()
From the documentation:
addListenerOnce(instance:Object, eventName:string, handler:Function) MapsEventListener Like addListener, but the handler removes itself after handling the first event.
Upvotes: 1
Reputation: 4962
This should solve it:
Example-
<input id="Button1" type="button" value="button" onclick="a()" />
<input id="Button2" type="button" value="button" onclick="b()" />
and
<script type="text/javascript">
var temp = true;
function a() {
if (temp == true) {
alert('1'); //Replace your function's code here
temp = false;
}
}
function b() {
if (temp == false) {
alert('2'); // Replace your function's code here
temp = true;
}
}
</script>
Upvotes: 1
Reputation: 83
Make 2 boolean flags (btn1Clicked, btn2Clicked)
, representing the state of the buttons (clicked or not), so you can procced Listening only if btn2Clicked
is true.
Upvotes: 0