简体   繁体   English

在openlayers地图上检测长按

[英]Detecting a long press on openlayers map

I have a map that presently catches the double-click event and fires some code to set a waypoint on the map and it works great. 我有一张地图,该地图目前捕获了双击事件并触发了一些代码以在地图上设置航点,因此效果很好。

What I'd really like to do is detect a long press for tablet/mobile users as I think the double-click method (which is working fine on my ipad) is not as precise as it is with a mouse pointer. 我真正想做的是检测平板电脑/移动设备用户的长按,因为我认为双击方法(在我的ipad上运行良好)不如使用鼠标指针那样精确。 I assume that I'll have to somehow tie in to onmousedown/onmouseup and set a timer/threshold but I'm struggling to get something like this working. 我认为我必须以某种方式绑定到onmousedown / onmouseup并设置一个计时器/阈值,但是我正在努力使这种工作正常进行。

Old question - but since I struggled with this a bit I figured i'd share the answer for people looking through google. 旧问题-但由于我为此感到有点挣扎,所以我想我会为那些通过google寻找信息的人分享答案。

var multiTouchEvent = 0;
var timeoutId;
var waitFor = 1000;
$(document).delegate("#openLayersMapDiv", "pagecreate", function() {
     map.events.register('touchstart', map, function(e) {
         multiTouchEvent = e.touches.length;
         timeoutId = setTimeout(function() {
              if (multiTouchEvent > 1) {
                   clearTimeout(timeoutId);
              }
              else {
                   alert("longpress!!!");
              }
         }, waitFor);
     }, true);

     map.events.register('touchmove', map, function(e) {
         clearTimeout(timeoutId);
     });
     map.events.register('touchend', map, function(e) {
         clearTimeout(timeoutId);
     });
}

The alert above will fire after 1000 miliseconds. 上面的警报将在1000毫秒后触发。 Change the waitFor value for how long you want the user to press the map. 更改waitFor值,您希望用户按下地图多长时间。

Explanation: Essentially the touchstart event fires an event after waiting 1000miliseconds (through the setTimeOut function). 说明:本质上,touchstart事件在等待1000毫秒后(通过setTimeOut函数)触发一个事件。 However if the user lifts up their finger (thus firing the touchend event) the setTimeOut function is cleared. 但是,如果用户抬起手指(从而触发touchend事件),则会清除setTimeOut功能。 This also happens if the user moves their finger (thus firing the touchmove event) or the zoom level changes (being monitored by multiTouchEvent variable). 如果用户移动手指(从而触发touchmove事件)或缩放级别发生变化(由multiTouchEvent变量监视),也会发生这种情况。

Hope this helps someone. 希望这对某人有帮助。

Tested on Android 4+ 在Android 4+上测试

try this code: 试试这个代码:

$(document).ready(function () {
    var longpress = false;

    $("button").on('click', function () {
        (longpress) ? alert("Long Press") : alert("Short Press");
    });

    var startTime, endTime;
    $("button").on('mousedown', function () {
        startTime = new Date().getTime();
    });

    $("button").on('mouseup', function () {
        endTime = new Date().getTime();
        longpress = (endTime - startTime < 500) ? false : true;
    });
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM