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

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

问题描述

我正在使用Google Maps和jQuery Mobile。



我可以很轻松地将点击事件绑定到地图上,但是有没有办法绑定长按?我想在长时间点击后在地图上添加一个标记。

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

  $('#map-canvas')。bind('taphold',function(e){
console.log('taphold');
e.stopImmediatePropagation();
return false;
});反过来,我可以使用Google地图的点击监听器,但是它可以获得短的点击次数,这使得地图很难用在手机上:

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

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

任何想法?

感谢您的帮助。

解决方案

对于任何正在寻找解决方案的人,我都会在Google地图论坛上获得此信息,它可以做到这一点。

 函数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;
}
新的LongClick(地图,300);
google.maps.event.addListener(map,'longpress',function(event){
do stuff ...
}


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.

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

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

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

Any ideas?

Thanks for your help.

解决方案

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天全站免登陆