向下移动键时,Google会更改点击行为 [英] Google maps changing click behaviour when shift key down

查看:45
本文介绍了向下移动键时,Google会更改点击行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让JS操作在按住Shift键的情况下在Google地图点击上发生变化.内置的click事件不提供键修饰符,因此您无法根据该事件做出决定.

I'm trying to have JS actions occur on a Google map click that differ when shift key is held down. The built-in click event doesn't give key modifiers, so you can't make decisions from that event.

但是您可以添加一个本机DOM侦听器,然后在此处进行检查:

But you can add a native DOM listener, and check there:

google.maps.event.addDomListener(document.getElementById("map"), 'click', function(event) {
    if (event.shiftKey) {
        // do something with x, y e.g. use an overlay to find latLng
    } 
    else {
        // do something else
    }
});

(改编自控制键+单击Google地图上的事件 )

但是,我仍然希望在适当的情况下触发正常的点击事件,例如,在单击地图的正常区域而不是在标记上时.

However, I still want the normal click event to fire, in the appropriate scenarios, e.g when clicking on a normal area of the map, but not on a marker.

    map.addListener( 'click', function(e) {
        // normal map click
    });

但是,此click事件在本机DOM事件之前触发,因此我看不到任何在本机事件中进行决策的方法,但仍保留了标准click的行为.

However, this click event fires BEFORE the native DOM event, so I can't see any way to make decisions in the native event but still keep the behavior of the standard click.

我错过了什么吗?例如,是否有办法使DOM事件在内置click事件之前触发?

Am I missing something? Is there a way, for example, to make the DOM event fire before the built-in click event?

推荐答案

也许很明显,但是我通过在常规click事件中设置全局变量,然后在DOM click事件中检查设置的变量来解决此问题

Perhaps it's obvious, but I solved this by simply setting a global variable in the normal click event, and then in the DOM click event checking for that variable being set.

这样,如果在适当的情况下可以触发正常的click事件,那么在Shift事件中(如果未按下shift键的情况下)设置变量并在DOM事件中对单击进行操作.

This way, if the circumstances are right for the normal click event to fire, then the variable is set and the click is acted on in the DOM event, if the shift key isn't down.

如果按下Shift键,我们将执行其他操作.

If the shift key is down, we do something else.

无论哪种方式,我们都清除变量.

Either way we clear the variable.

var mapClickFired = false;

map.addListener( "click", function(e) {
    mapClickFired = true; // acted on in DOM listener below
});

// bind to DOM event as well, so we can find out if modifier keys are pressed
google.maps.event.addDomListener( document.getElementById("map"), "click", function(event) {
    // check modifiers
    if ( event.shiftKey ) {
        // do things for shift key down
    }
    else if ( mapClickFired ) {
        // do things for regular map click 
    }
    mapClickFired = false;
});

当然,这确实取决于这两个事件的顺序是否与预期一致.由于没有记录,所以我有点紧张,我创建了一些脆弱的东西.

Of course, this does depend on the ordering of these two events being consistently as expected. Since it's not documented I'm a bit nervous I've created something fragile.

这篇关于向下移动键时,Google会更改点击行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆