在 jQuery Mobile 中对 Google 地图使用 longclick/taphold? [英] Using longclick/taphold with Google Maps in jQuery Mobile?

查看:29
本文介绍了在 jQuery Mobile 中对 Google 地图使用 longclick/taphold?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Google 地图与 jQuery Mobile 结合使用.

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 the map following a long click.

我可以使用 jQuery Mobile 的taphold",它专为长时间点击而设计,但这并不能让我访问地图属性,例如点击的经纬度:

I can use jQuery Mobile's 'taphold', designed especially for long clicks, but that doesn't give me a way to access map properties such as the latlng of the tap:

    $('#map-canvas').bind('taphold', function(e) {
      console.log('taphold');
      e.stopImmediatePropagation();
      return false;
    } );

相反,我可以使用 Google 地图点击侦听器,但它会接收短点击,这使得地图在移动设备上使用起来非常方便:

Conversely, I can use the Google Maps click listener, but that picks up short clicks, which makes the map fiddly to use on a mobile:

google.maps.event.addListener($('#map-canvas'), 'click', function(event){  ...

我没有看到 Google Maps API V3 的longclick"事件侦听器:http://code.google.com/apis/maps/documentation/javascript/reference.html#Map

I don't see a 'longclick' event listener for Google Maps API V3: http://code.google.com/apis/maps/documentation/javascript/reference.html#Map

有什么想法吗?

感谢您的帮助.

推荐答案

对于任何正在寻找解决方案的人,我都在 Google 地图论坛上找到了这个,它确实解决了问题.

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

这篇关于在 jQuery Mobile 中对 Google 地图使用 longclick/taphold?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆