Question

can we ?

i want to do something like this:

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

how to catche the 'db_right_click' event ??

is there any way ??

Was it helpful?

Solution

i get it

and

this is my code:

//***********  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 ********/

OTHER TIPS

I couldn't see an easy way to capture the double right click event on google maps.

But here's the work around I used for my use case:

What I needed was to perform an action on a single right click, but not perform that action on a double right click, just let the map zoom out like normal.

I also noticed that google maps emits just 1 rightclick event even when you double right click.

So all I did was listen on google's rightclick event, and then setTimeout for 300 milliseconds and if the new zoom was different than the old zoom, that means they double right clicked, else perform my action:

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

You can catch double right click event by JavaScript, but it's not enough if you need some specific data from google maps event. For example, you need to get coordinates of the place that was double right clicked. Here is solution:

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

You can get any google map event property in the same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top