Android map 获取滚动事件

标签 android maps

我目前正在使用 Andoid Maps SDK 开发应用。

现在,如果用户滚动 map 以从基于新 map 中心的服务器加载其他标记,我想收到通知。

我已经搜索了注册监听器的函数,但我没有找到任何东西。

有什么方法可以了解 map 中心的变化吗?我不想为此实现轮询机制...... :(

最佳答案

我用两种方式做到了这一点:

触摸监听器。为 map View 设置触摸监听器。每次用户抬起手指(或移动或触碰)时,您都可以重新加载。

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。 每次 map 移动时,都会调用 onLayout。如果您扩展 MapView 类,您可以覆盖 onLayout 来捕获这些事件。我在这里设置了一个计时器,看看运动是否停止了。

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

您也可以在触摸监听器方法中使用计时器。这只是一个例子。希望对您有所帮助!

关于Android map 获取滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672655/

相关文章:

Android mapbox 库非常大

java - 当我用抽屉导航实现它时,SupportMapFragment 为空

c++ - 在内存中实现无限 map

java - 折线未清除

android - 无法使用 fragment 播放多个视频

java - 按顺序从 Java 属性文件中提取值?

javascript - 关闭Google Maps API v3中POI的信息窗口?

Android - 链接到其他应用程序商店?

java - 有没有可靠的方法来处理来自 Android 应用程序的 Windows 集成 (NTLM) 身份验证?

iphone - 音乐网站 : how to make it work on android/blackberry/iphone/symbian?