【问题标题】:can we catch the 'double right click' event on google-maps-v3?我们可以在 google-maps-v3 上捕捉到“双击右键”事件吗?
【发布时间】:2010-04-02 04:01:32
【问题描述】:

我们可以吗?

我想做这样的事情:

google.maps.event.addListener(map, 'db_right_click', function(event) {
   alert('ssss')
}

如何捕捉 'db_right_click' 事件??

有什么办法吗??

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    我明白了

    这是我的代码:

    //***********  double right click ********/ 
                                    var c =0 ;
                                    $('#map_canvas')[0].onmousedown=function(event){
                                        if(event.button == 2){
                                            c++;
                                    setTimeout(cc, 600);
                                    }
                                 if (c >1){
                                       alert('ok i get it')
                                    }
                                    }
                                    function cc{
                                        c=0;
                                    }
                                  //***********  double right click ********/
    

    【讨论】:

      【解决方案2】:

      我看不到在谷歌地图上捕获双击右键事件的简单方法。

      但这是我用于我的用例的解决方法:

      我需要的是在一次右键单击时执行一个操作,而不是在双击右键时执行该操作,只是让地图像平常一样缩小。

      我还注意到,即使您双击右键,谷歌地图也只会发出 1 个右键单击事件。

      所以我所做的就是监听 google 的右键单击事件,然后 setTimeout 300 毫秒,如果新的缩放与旧的缩放不同,这意味着他们双击右键,否则执行我的操作:

      google.maps.event.addListener(map, "rightclick", function(event) {
          var zoom = map.getZoom()
            setTimeout(function () {
            if (map.getZoom() == zoom) { // zooms are the same so they didn't double right click.
              handleSingleRightClick() // <-- your code here
            }
          }, 300)
      });
      

      【讨论】:

        【解决方案3】:

        您可以通过 JavaScript 捕获双击右键事件,但如果您需要来自 google maps 事件的一些特定数据,这还不够。例如,您需要获取双击右键的位置的坐标。这是解决方案:

        // variable to store right clicked place coordinates
        let rightClickCoordinates;
        // variable to store amounts of right clicks
        let rightClicksAmount = 0;
        
        // listener for google map right click event
        google.maps.event.addDomListener(map, 'rightclick', (e) => {
            // store coordinates of place was right clicked
            rightClickCoordinates = e.latLng;
        });
        
        // listener for google map container double right click event
        document.getElementById('your_map_container_id').addEventListener('mousedown', () => {
            // check for double right click
            if(e.button == 2) {
                // if right mouse button clicked increase amount by 1
                rightClicksAmount++;
            }
        
            // if we had more than 1 right click during the 300 ms - we caught right double click event
            if (rightClicksAmount > 1) {
                // do what you want with coordinates
                console.log(rightClickCoordinates.lat() + ', ' + rightClickCoordinates.lng());
            }
        
            // after 300ms set right clicks amount to 0
            setTimeout(()=> {
                rightClicksAmount = 0;
            }, 300);
        }});
        

        您可以通过同样的方式获取任何谷歌地图事件属性。

        【讨论】:

          猜你喜欢
          • 2014-01-05
          • 2011-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-27
          • 2012-10-27
          • 1970-01-01
          • 2013-07-25
          相关资源
          最近更新 更多