2

したがって、それぞれが異なる機能を実行する2つのhtmlボタンがあります(両方の機能は以下にあります)。基本的に、2 つのボタンのいずれかをクリックして、Google マップ アクションリスナーをマップに追加します。私はそれをうまく機能させました。唯一の問題は、アクションリスナーをワンクリックで利用できるようにしたいということです。その1回のクリックの後、アクションリスナーが再び「聞く」前に、ユーザーが別のボタンをクリックする必要があります。それが理にかなっていることを願っています。

    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」に渡しました。

これは、最初のクリック後にすべてのアクションリスナーを無効にするように機能します。ただし、ボタンをもう一度クリックしてもリセットされません。

    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");
                }
        });
    }
}
4

3 に答える 3

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>
于 2012-10-27T04:16:44.873 に答える
1

使用するgoogle.maps.event.addListenerOnce()

ドキュメントから:

addListenerOnce(instance:Object, eventName:string, handler:Function) MapsEventListener addListener と同様ですが、ハンドラは最初のイベントの処理後に自身を削除します。

于 2012-10-27T06:10:19.320 に答える
0

ボタンの状態 (クリックされたかどうか) を表す2 つのブール値フラグ(btn1Clicked, btn2Clicked)を作成して、true の場合にのみリスニングを処理できるようにしますbtn2Clicked

于 2012-10-27T03:09:48.330 に答える