所以我有两个html按钮,每个按钮运行不同的功能(两个功能都在下面)。基本上,您单击两个按钮之一即可将Google Maps actionlistener添加到地图中。我已经成功地开始工作了。唯一的问题是我只想让actionlistener可以点击一下。在单击之后,我希望用户必须在actionlistener再次“侦听”之前单击另一个按钮。我希望这是有道理的。
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);
});
};
所以我试过这个:
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");
}
};
它不起作用.....请指出我正确的方向...... (顺便说一下,按钮将“true”传递给'setBoolean'。
这可以在第一次单击后禁用所有actionlisteners。但是再次单击按钮后它不会重置。
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");
}
});
}
}
答案 0 :(得分:1)
这应解决它:
实施例 -
<input id="Button1" type="button" value="button" onclick="a()" />
<input id="Button2" type="button" value="button" onclick="b()" />
和
<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>
答案 1 :(得分:1)
使用google.maps.event.addListenerOnce()
addListenerOnce(instance:Object,eventName:string,handler:Function) MapsEventListener与addListener类似,但处理程序会自行删除 处理完第一个事件后。
答案 2 :(得分:0)
制作2个布尔标志(btn1Clicked, btn2Clicked)
,表示按钮的状态(单击与否),因此只有在btn2Clicked
为真时才能获得侦听。