在谷歌地图v3多边形上检测鼠标事件的shiftKey [英] Detect shiftKey for mouse event on google maps v3 polygon

查看:168
本文介绍了在谷歌地图v3多边形上检测鼠标事件的shiftKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用google maps V3(.16)JavaScript API时,有没有办法在多边形上检测shift-mouseclick?
https://developers.google.com/maps/documentation/javascript / reference#MouseEvent



我收到鼠标事件,并且可以看到原始事件被封装在一个Google事件类中,这就是需要(访问事件。 Ua .shiftKey属性)。



然而,我发现上个月,谷歌可能已经更新了他们的api,属性重命名为事件。 Pa .shiftKey。

有关参考资料,请参阅 http:// i.stack.imgur.com/80npD.png 获取webinspector中事件结构的屏幕截图。
有没有什么办法可以检测在使用事件点击谷歌地图多边形期间是否按下了shift键,而不必依赖google-not-updating-their-api?我错过了什么?



Thx!

解决方案

使用未记录的属性几乎总是一个坏主意。如果你可以避免它,避免它。在这种情况下,API可能正在使用Closure Compiler进行压缩,所以下次更新时,它可能不再是 Pa


有没有什么方法可以检测在使用事件点击谷歌地图多边形期间是否按下了shift键,而无需依赖google-not-如果只是 Ua 与<$ c






$ b

您可以检测:

  var shiftKey =(event.Ua || event.Pa).shiftKey; 

使用JavaScript的好奇地强大 || 运算符来引用 Ua 属性的值,如果有的话,它是真的,否则 Pa 属性的值。

假设当他们将它从 Ua 更改为 Pa 时,它们不会使用 Ua 作其他用途。如果有机会,这个更彻底的版本可以做到这一点:

  var shiftKey =((event.Ua&& amp ;'shiftKey'in event.Ua)?event.Ua:event.Pa).shiftKey; 

具体检查 Ua 对象 shiftKey 属性,如果未找到,则回落到 Pa



如果它可以是任何东西



...您可以搜索它:

  var shiftKey; 
if(event.key(event).some(function)(key){
if(事件[key]中的event [key]&'shiftKey'){
shiftKey =事件[key] .shiftKey;
返回true;
}
返回false;
})){
//我们发现它,`shiftKey`的值是
} else {
//我们没有找到它
}

注意: Object.keys 是所有现代浏览器上的ES5功能。如果您需要支持IE8等较旧的浏览器,则可以进行填充。


Is there a way to detect a shift-mouseclick on a polygon when using the google maps V3(.16) javascript API? https://developers.google.com/maps/documentation/javascript/reference#MouseEvent

I do receive the mouse event, and I can see that the original event is wrapped in a google event class, which is just what's needed (Access the event.Ua.shiftKey property).

I found however that last month, google may have updated their api and now the property is renamed to event.Pa.shiftKey.

For reference, see http://i.stack.imgur.com/80npD.png for a screenshot of the event structure in the webinspector. Is there any way to detect whether the shift key is pressed during a click on a google maps polygon using the event, and without having to rely on google-not-updating-their-api? What am I missing?

Thx!

解决方案

Using undocumented properties is almost always a bad idea. If you can possibly avoid it, avoid it. In this case, the API is probably being compressed with the Closure Compiler, and so the next time they update, it may not be Pa anymore either.

Is there any way to detect whether the shift key is pressed during a click on a google maps polygon using the event, and without having to rely on google-not-updating-their-api?

If just Ua vs. Pa:

You could feature detect:

var shiftKey = (event.Ua || event.Pa).shiftKey;

That uses JavaScript's curiously powerful || operator to reference the Ua property's value, if there is one and it's truthy, or the Pa property's value otherwise.

This assumes that when they change it from Ua to Pa, they don't use Ua for something else. If there's a chance of that, this more thorough version would do it:

var shiftKey = ((event.Ua && 'shiftKey' in event.Ua) ? event.Ua : event.Pa).shiftKey;

That specifically checks for a Ua object with a shiftKey property, falling back to Pa if not found.

If it could be anything

...you can search for it:

var shiftKey;
if (Object.keys(event).some(function(key) {
      if (event[key] && 'shiftKey' in event[key]) {
          shiftKey = event[key].shiftKey;
          return true;
      }
      return false;
  })) {
    // We found it, `shiftKey` has the value
} else {
    // We didn't find it
}

Side note: Object.keys is an ES5 feature present on all modern browsers. If you need to support older browsers like IE8, it can be polyfilled.

这篇关于在谷歌地图v3多边形上检测鼠标事件的shiftKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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