Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Maps get Scroll Event

Tags:

android

maps

i am currently developing an app with the Andoid Maps SDK.

Now i would like to get a notice if the user scrolls the map to load additional markers from a server based on the new map center.

I already searched for a function to register a listener but i didn´t find anything.

Is there any way to get informed about map center changes? I don´t want to implement a polling mechanism for that... :(

like image 540
beck Avatar asked Sep 08 '10 23:09

beck


2 Answers

I have done this in two ways:

Touch listener. Set the on touch listener for the map view. Every time the user lifts their finger (or moves, or touches down), you can reload.

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

Override onLayout. Every time the map moves, onLayout is called. If you extend the MapView class, you can override onLayout to catch these event. I set a timer in here to see whether the movement had stopped.

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

You can use the timer in the touch listener method as well. This was just an example. I hope this helps!

like image 147
Tim Mahoney Avatar answered Oct 14 '22 06:10

Tim Mahoney


Take a look at the following blog post (comes with Github code): http://bricolsoftconsulting.com/extending-mapview-to-add-a-change-event/

like image 32
Theo Avatar answered Oct 14 '22 05:10

Theo