Using longclick/taphold with Google Maps in jQuery Mobile?

前端 未结 5 1985
暖寄归人
暖寄归人 2020-12-28 22:21

I\'m using Google Maps with jQuery Mobile.

I can bind a click event to the map easily enough, but is there a way to bind a long click? I\'d like to add a marker to

5条回答
  •  青春惊慌失措
    2020-12-28 22:50

    For anyone looking for a solution, I got this on the Google Maps forums, and it does the trick.

    function LongClick(map, length) {
        this.length_ = length;
        var me = this;
        me.map_ = map;
        google.maps.event.addListener(map, 'mousedown', function(e) { me.onMouseDown_(e) });
        google.maps.event.addListener(map, 'mouseup', function(e) { me.onMouseUp_(e) });   
    }   
    LongClick.prototype.onMouseUp_ = function(e) {
        var now = +new Date;
        if (now - this.down_ > this.length_) {
            google.maps.event.trigger(this.map_, 'longpress', e);
        }   
    }   
    LongClick.prototype.onMouseDown_ = function() {
        this.down_ = +new Date;   
    }
    new LongClick(map, 300);
    google.maps.event.addListener(map, 'longpress', function(event) {
        do stuff...
    }
    

提交回复
热议问题