Handling ctrl + click on Google Maps

I am trying to get the user to select several markers on my map by pressing the control key and clicking the marker.

For this, I write this code:

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

GoogleMaps V3 docs has no information or documentation about this object eother than a property latLng. But when I debug with Google Chrome, I see this object Rathat contains exactly what I need. My question is: is it safe to hardcode this undocumented access Rato receive by pressing ctrlKey?

enter image description here

+4
source share
2 answers

, Ra, ? , google /.


, , , ctrl :

β†’ http://jsfiddle.net/FbGa5/
. iframe, ctrl .

ctrl:

var selecting = false,
    selectedMarkers = [];

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

, ctrl , . , , , :

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');
   }
});

: Ra , .

+7

, - . , , , - MouseEvent . , , ,

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 ....

}
+1

Source: https://habr.com/ru/post/1526764/


All Articles