我目前正在开发一个带Andoid Maps SDK的应用程序。Android地图获取滚动事件
现在我想获得一个通知,如果用户滚动地图从基于新地图中心的服务器加载额外的标记。
我已经搜索了一个函数来注册一个监听器,但是我没有找到任何东西。
有什么方法可以获得关于地图中心更改的信息吗?我不希望实现为轮询机制... :(
我目前正在开发一个带Andoid Maps SDK的应用程序。Android地图获取滚动事件
现在我想获得一个通知,如果用户滚动地图从基于新地图中心的服务器加载额外的标记。
我已经搜索了一个函数来注册一个监听器,但是我没有找到任何东西。
有什么方法可以获得关于地图中心更改的信息吗?我不希望实现为轮询机制... :(
我已经在两个方面做到了这一点:。
触摸听者设置触摸侦听器地图视图每次用户提起他们的手指(或移动,或触地),你可以重新加载。
mapView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// The user took their finger off the map,
// they probably just moved it to a new place.
break;
case MotionEvent.ACTION_MOVE:
// The user is probably moving the map.
break;
}
// Return false so that the map still moves.
return false;
}
});
覆盖onLayout。每次地图上移动,onLayout被调用。如果扩展MapView类,则可以覆盖onLayout来捕捉这些事件。我在这里设置了一个计时器来查看movem是否正确ent已经停止。
public class ExtendedMapView extends MapView {
private static final long STOP_TIMER_DELAY = 1500; // 1.5 seconds
private ScheduledThreadPoolExecutor mExecutor;
private OnMoveListener mOnMoveListener;
private Future mStoppedMovingFuture;
/**
* Creates a new extended map view.
* Make sure to override the other constructors if you plan to use them.
*/
public ExtendedMapView(Context context, AttributeSet attrs) {
super(context, attrs);
mExecutor = new ScheduledThreadPoolExecutor(1);
}
public interface OnMoveListener {
/**
* Notifies that the map has moved.
* If the map is moving, this will be called frequently, so don't spend
* too much time in this function. If the stopped variable is true,
* then the map has stopped moving. This may be useful if you want to
* refresh the map when the map moves, but not with every little movement.
*
* @param mapView the map that moved
* @param center the new center of the map
* @param stopped true if the map is no longer moving
*/
public void onMove(MapView mapView, GeoPoint center, boolean stopped);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mOnMoveListener != null) {
// Inform the listener that the map has moved.
mOnMoveListener.onMove(this, getMapCenter(), false);
// We also want to notify the listener when the map stops moving.
// Every time the map moves, reset the timer. If the timer ever completes,
// then we know that the map has stopped moving.
if (mStoppedMovingFuture != null) {
mStoppedMovingFuture.cancel(false);
}
mStoppedMovingFuture = mExecutor.schedule(onMoveStop, STOP_TIMER_DELAY,
TimeUnit.MILLISECONDS);
}
}
/**
* This is run when we have stopped moving the map.
*/
private Runnable onMoveStop = new Runnable() {
public void run() {
if (mOnMoveListener != null) {
mOnMoveListener.onMove(ExtendedMapView.this, getMapCenter(), true);
}
}
};
}
您也可以在触摸侦听器方法中使用该定时器。这只是一个例子。我希望这有帮助!
看看下面的博客文章(自带Github的代码): http://bricolsoftconsulting.com/extending-mapview-to-add-a-change-event/
它不起作用 – 2012-11-18 15:27:41
对于很多人来说,它确实有效,正如您可以从该页面的评论中看到的那样。它可能不适用于您,具体取决于您的Android操作系统和/或设备。但留下一个模糊的评论,并给我一个downvote不会帮助任何人。如何在这里进行富有建设性的对话,让我们知道您使用的Android设备和版本是什么? – Theo 2012-11-19 02:29:08
我在4.1上使用Galaxy Nexus,在4.0上使用Galaxy Note 10.1,组件很少引发更改事件。它需要花费很多时间并滚动,直到它引发第一个变化事件,然后再次停止工作。处理来自标准MapView的触摸事件监听器要可靠得多。所以它不起作用,我的用户不会被绑定到作者正在测试的一台设备上。 – 2012-11-19 11:58:39
浪费了那么多时间this.None方法作品。 downvote是必要的。 – Nezam 2014-05-02 02:19:08