Google Maps Api V3: click event firing twice

前端 未结 5 1305
鱼传尺愫
鱼传尺愫 2021-01-14 05:43

I am following the MVCObject binding example from this page:

http://code.google.com/apis/maps/articles/mvcfun.html

I want to change the color of the circle a

5条回答
  •  失恋的感觉
    2021-01-14 06:15

    To actually find out what is calling the function, use console.log(event) and check the developer tools console:

    google.maps.event.addListener(circle, 'click', function()
    {
        alert('circle clicked');
        console.log(event);
    });
    

    You should have 2 events listed (if they do trigger twice). Then check the "srcElement" attribute of the events to see what element had called the function.

    From a similar issue of my own, I found that the Google maps event listener was not specific enough on what element would trigger the function. So I replaced the Google maps event listener with a JQuery event listener to be more specific and target a specific element by it's ID attribute.

    Google Maps event listener (fired twice)

    google.maps.event.addDomListener(control, 'click', function() { toggleLabels(); });
    

    JQuery event listener (fired once)

    $('body').on('click', '#labels-switch', function(){ toggleLabels(); });
    

提交回复
热议问题