知道什么时候的地图已经停止滚动(如" moveend"在JavaScript API)什么时候、地图、QUOT、API

2023-09-05 07:54:09 作者:别在机场等一艘船丷

我需要检测何时图形页面已滚动或放大,如JavaScript的API中的moveend事件。我想等到视图停止移动,这样我就可以检测,如果我需要查询我的服务器项目withing观看矩形,如果是发送一个请求。 (其实我送了一个比观看矩形稍大面积的要求)

I need to detect when a MapView has been scrolled or zoomed, like the "moveend" event in the javascript API. I'd like to wait until the view has stopped moving, so I can then detect if I need to query my server for items withing the viewing rectangle, and if so send out a request. (actually I send a request for a slightly larger area than the viewing rectangle)

很显然,我宁愿不发送数据请求,如果认为还在动。但更糟糕的是,我不知道,我需要发送另一个请求,离开地图缺少标记的区域。

Obviously, I'd rather not send out a request for data if the view is still moving. But even worse is that I don't know that I need to send another request, leaving areas of the map missing markers.

目前,我继承图形窗口和处理的onTouchEvent如下:

Currently I am subclassing MapView and handling the onTouchEvent as follows:

 public boolean onTouchEvent(android.view.MotionEvent ev) {
        super.onTouchEvent (ev);
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            GeoPoint center = getMapCenter();
            int latSpan = getLatitudeSpan(), lngSpan = getLongitudeSpan();
            /* (check if it has moved enough to need a new set of data)  */    
        }
        return true;
    }

但问题是,我不知道,如果认为已经停止,因为滚动往往具有惯性,并能保持持续过去的ACTION_UP事件。

Problem is, I don't know if the view has stopped, since scrolling tends to have inertia and can keep going past the "ACTION_UP" event.

有一些事件,我可以挖掘到这会提醒我,当一个图形页面完成移动(或缩小)?如果没有,有没有人写的逻辑来检测呢?从理论上讲,我可以做一个猜测通过查看所有动作,并设置一些后来一起走位,并检查它......但似乎凌乱和PITA。但是,如果有人已经写好了....:)

Is there some event I can tap into that will alert me when a mapview is done moving (or zooming)? If not, has anyone written logic to detect this? In theory I could make a guess by looking at all the actions, and set something to come along bit later and check it...but...that seems messy and a PITA. But if someone has already written it.... :)

推荐答案

我真的没有一个令人满意的解决这个问题,但是我可以告诉我做了什么,以部分解决这个问题。

I don't really have a satisfactory solution to this problem, but I can tell what I did to partially solve it.

在我的子类图形页面并覆盖了 computeScroll()方法,该方法获取当前中心点地图与上次已知中心点(作为挥发性在子类字段存储)进行比较。如果中心点发生了变化,它触发一个事件映射的监听器(我定义的自定义侦听器接口这一点)。 监听器是实例化的AsyncTask 的子类,执行它的活动。此任务会暂停在其 doInBackGround() 100ms的方法,执行服务器取数据之前。 当监听活动得到第二张地图移动事件(它会做的,因为地图上移动的步进作用),它会检查的状态刚刚执行的AsyncTask 。如果任务仍在运行,它会取消()它。然后创建一个新的任务,并执行。 I subclassed MapView and overrode the computeScroll() method, which gets the current centre-point of the map and compares it with the last-known centre-point (stored as a volatile field in the subclass). If the centre-point has changed, it fires an event to the listener of the map (I defined a custom listener interface for this). The listener is an activity that instantiates a subclass of AsyncTask and executes it. This task pauses for 100ms in its doInBackGround() method, before performing the server data fetch. When the listener activity receives a second map-move event (which it will do because of the stepping effect of the map movement), it checks the status of the just-executed AsyncTask. If that task is still running, it will cancel() it. It then creates a new task, and executes that.

总的效果是,当听众获得的地图移动的事件乱舞几毫秒分开时,只有一个,实际上触发要执行的任务的服务器取是最后一个序列中。缺点是,它引入了地图上的移动之间有轻微的延迟发生,服务器获取的发生。

The overall effect is that when the listeners get the flurry of map-moved events a few milliseconds apart, the only one that actually triggers the task to perform the server-fetch is the last one in the sequence. The downside is that it introduces a slight delay between the map movement happening, and the server fetch occurring.

我不喜欢它,它的丑陋,但它缓解了这一问题。我的爱的看到一个更好的解决方案了这一点。

I'm not happy with it, it's ugly, but it mitigates the problem. I would love to see a better solution to this.