简体   繁体   English

处理ctrl +点击谷歌地图

[英]Handling ctrl+click on Google Maps

I'm trying to make the user select multiple markers on my map by pressing control key and click on the marker. 我正在尝试让用户通过按下控制键并点击标记在地图上选择多个标记。

For doing this, I write this code: 为此,我写了这段代码:

google.maps.event.addListener(marker, 'click', function (e) {
    // detect if is pressed ctrlKey or not to do stuff
}

In the GoogleMaps V3 docs there is no info or documentation about this e object, beyond the latLng property. 在GoogleMaps V3 文档中 ,除了latLng属性之外,没有关于此e对象的信息或文档。 But when I debug with Google Chrome, I see this Ra object that contains exactly what I needed. 但是当我使用谷歌浏览器进行调试时,我看到这个Ra对象包含了我需要的内容。 My question is, it is safe to hardcode this undocumented Ra access to obtain if ctrlKey is pressed? 我的问题是,如果按下ctrlKey,可以安全地对这个未记录的Ra访问进行硬编码吗?

在此输入图像描述

As asked for in comment, what contains Ra that is important? 正如评论中所要求的,包含Ra的重要内容是什么? My experience is, that google maps change those internal variable / object names constantly. 我的经验是,Google地图会不断更改这些内部变量/对象名称。


However, had made a demo to show how to select multiple markers by holding ctrl down and clicking on the markers : 但是,做了一个演示,通过按住ctrl并单击标记来显示如何选择多个标记:

See fiddle -> http://jsfiddle.net/FbGa5/ 看小提琴 - > http://jsfiddle.net/FbGa5/
Notice : You must activate the iframe by clicking on the map once before ctrl or any other keypress can be captured. 注意 :您必须在ctrl之前单击地图一次激活iframe,或者可以捕获任何其他按键。

Keep track of the ctrl key : 跟踪ctrl键:

var selecting = false,
    selectedMarkers = [];

window.onkeydown = function(e) {
  selecting = ((e.keyIdentifier == 'Control') || (e.ctrlKey == true));
}
window.onkeyup = function(e) {
  selecting = false;
}

Select markers if ctrl -key is down and a marker is clicked. 如果ctrl -key关闭且单击标记,请选择标记。 If a marker is selected, it turns blue, if a marker is deselected it turns back to red : 如果选择了标记,它将变为蓝色,如果取消选择标记,则会变回红色:

google.maps.event.addListener(marker, 'click', function() {
   if (!selecting) return;
   var id = this.id;
   var index = selectedMarkers.indexOf(id);
   if (index>-1) {
     this.setIcon('https://maps.gstatic.com/mapfiles/ms2/micons/red-dot.png');
     selectedMarkers.splice(index, 1);
   } else {
     selectedMarkers.push(id);             
     this.setIcon('https://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png');
   }
});

Conclusion : You do not need Ra or any other argument in the marker click event in order to get it to work. 结论:您无需在标记点击事件中使用Ra或任何其他参数即可使其工作。

I guess that the class members are changed due to minification. 我猜团体成员因缩小而改变了。 My idea, that i know is not perfect, is to look for the MouseEvent by type and not by name. 我的想法,我知道并不完美,是按类型而不是按名称查找MouseEvent。 Then, when found , give it a constant name that you know 然后,找到后,给它一个你知道的常数名称

google.maps.event.addListener(marker, 'click', function (e) {
    // detect if is pressed ctrlKey or not to do stuff

   //detect the mouse event member by type
    //and always give it the name "mouseEvent"         
    for (var key in e) {
        if (e[key] instanceof MouseEvent) {
           e["mouseEvent"] = e[key];
           break;
         }
     }

   // e.mouseEvent.ctrlKey ....

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM