How to detect long press on Google map markers (javascript)?

I think there is no long print / click event handler on Google maps. Is the code below a way to implement this?

+4
source share
3 answers

I took advantage of the long-press function using the mouse and mouse event listener. Thank you !!

var longpress = false;

    google.maps.event.addListener(marker,'click', function (event) {
            (longpress) ? console.log("Long Press") : console.log("Short Press");
        });



    google.maps.event.addListener(marker, 'mousedown', function(event){

                start = new Date().getTime();           
            });

    google.maps.event.addListener(marker, 'mouseup', function(event){

                end = new Date().getTime();
                    longpress = (end - start < 500) ? false : true;         

            });
+8
source

I know this is an old post, but I ran into the same problem and I found what I consider to be a better solution than the one suggested by @Anbarasan Thangapalam.

Decision:

var mousedUp = false;
google.maps.event.addListener(marker, 'mousedown', function(event){ 
    mousedUp = false;
    setTimeout(function(){
        if(mousedUp === false){
            //do something if the mouse was still down
            //after 500ms        
        }
    }, 500);
});
google.maps.event.addListener(marker, 'mouseup', function(event){ 
    mousedUp = true;
});

I think this approach can be better understood, with fewer lines of code and fewer listener functions.

- , mouseup dragstart. :

google.maps.event.addListener(marker, 'dragstart', function(event){ 
    mousedUp = true;
});
+4

For me, this method worked. To find out if this is rightclickor longPressjust check if the event type is "contextmenu" inside your handler.

gMaps.event.addListener(overlay, 'click', function() {
    if(event.type === "contextmenu"){
     //longpress handler codes
    }
}
0
source

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


All Articles