开发者

How can I detect if an Android MapView has been panned or zoomed?

I'm creating an Android app that searches for items based on the visible area of the MapView. Is there a w开发者_运维知识库ay to set up a listener on my MapView to detect when a map has been panned or zoomed?


try mapview-overlay-manager, it is a extension for overlayer for android maps,

it has some simplified OnGestureListener, few example:

onSingleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onDoubleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onLongPress(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onZoom(ZoomEvent, ManagedOverlay)

onScrolled(...)

link:http://code.google.com/p/mapview-overlay-manager/ hope it helps


You can create a SimpleMapView that extends MapView.

public class SimpleMapView extends MapView {

    private int currentZoomLevel = -1;
    private GeoPoint currentCenter;
    private List<ZoomChangeListener> zoomEvents = new ArrayList<ZoomChangeListener>();
    private List<PanChangeListener> panEvents = new ArrayList<PanChangeListener>();

    public SimpleMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SimpleMapView(Context context, String apiKey) {
        super(context, apiKey);
    }

    public SimpleMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 
     * @return
     */
    public int[][] getBounds() {

        GeoPoint center = getMapCenter();
        int latitudeSpan = getLatitudeSpan();
        int longtitudeSpan = getLongitudeSpan();
        int[][] bounds = new int[2][2];

        bounds[0][0] = center.getLatitudeE6() - (latitudeSpan / 2);
        bounds[0][1] = center.getLongitudeE6() - (longtitudeSpan / 2);

        bounds[1][0] = center.getLatitudeE6() + (latitudeSpan / 2);
        bounds[1][1] = center.getLongitudeE6() + (longtitudeSpan / 2);
        return bounds;
    }

    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            GeoPoint centerGeoPoint = this.getMapCenter();
            if (currentCenter == null || 
                    (currentCenter.getLatitudeE6() != centerGeoPoint.getLatitudeE6()) ||
                    (currentCenter.getLongitudeE6() != centerGeoPoint.getLongitudeE6()) ) {
                firePanEvent(currentCenter, this.getMapCenter());
            }
            currentCenter = this.getMapCenter();
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(getZoomLevel() != currentZoomLevel){
            fireZoomLevel(currentZoomLevel, getZoomLevel());
            currentZoomLevel = getZoomLevel();
        }
    }

    @Override
    public void setSatellite(boolean on){
        super.setSatellite(on);
    }

    @Override
    public MapController getController(){
        return super.getController();
    }

    private void fireZoomLevel(int old, int current){
        for(ZoomChangeListener event : zoomEvents){
            event.onZoom(old, current);
        }
    }

    private void firePanEvent(GeoPoint old, GeoPoint current){
        for(PanChangeListener event : panEvents){
            event.onPan(old, current);
        }
    }

    public void addZoomChangeListener(ZoomChangeListener listener){
        this.zoomEvents.add(listener);
    }

    public void addPanChangeListener(PanChangeListener listener){
        this.panEvents.add(listener);
    }
}

You have the Listeners you can put the code for pan or zoom. Then in your xml:

 <com.androidnatic.maps.SimpleMapView android:clickable="true" 
                    android:layout_height="match_parent" android:id="@+id/mapView" 
                    android:layout_width="match_parent"
                     android:apiKey="xxx">
 </com.androidnatic.maps.SimpleMapView>

And then in your code you can specify the Pan Listener:

mapView.addPanChangeListener(new PanChangeListener() {

            @Override
            public void onPan(GeoPoint old, GeoPoint current) {
                          //TODO
                        }
        });


Sadly, there is no built-in functionality to do this in the MapView tools (a strange oversight since this functionality is in the JavaScript SDK, as well as the iOS SDK).

You can deal with this easily enough though by using a Runnable and just polling the MapView. I do this by keeping track of the "last" state of:

getLatitudeSpan();
getLongitudeSpan();
getCenter();
getZoomLevel();

And then comparing them to the current values. If the values of changed, you know the map view has moved. If not, you can do nothing.

Either way, reschedule the runnable for another run after 500ms or so and repeat the process. You can use onResume() and onPause() to remove the callback for the Runnable and restart it as necessary.


The MapView class can track changes using the onLayout method.

i.e.

class CustomMapView extends MapView {

    protected void onLayout(boolean changed,int left, int right, int top, int bottom){
        super.onLayout(changed,left, right, top, bottom);
        if(changed){
        // do something special
        }
    }
}


The only way that I can think of is to extends the MapView and override the OnTouchEvent and watch for the Up action. This will tell you that the user has finished moving and you can get the lat/lon span to determine the region you should check out.


Old question, but I wrote a class a while back called ExMapView which fires an event when the map region starts changing (onRegionBeginChange) and when it stops changing (onRegionEndChange). This class is for use with the old MapView, not V2.0. Hope it helps someone.

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class ExMapView extends MapView {
    private static final String TAG = ExMapView.class.getSimpleName();
    private static final int DURATION_DEFAULT = 700;

    private OnRegionChangedListener onRegionChangedListener;
    private GeoPoint previousMapCenter;
    private int previousZoomLevel;
    private int changeDuration; // This is the duration between when the user stops moving the map around and when the onRegionEndChange event fires.
    private boolean isTouched = false;
    private boolean regionChanging = false;

    private Runnable onRegionEndChangeTask = new Runnable() {
        public void run() {
            regionChanging = false;
            previousMapCenter = getMapCenter();
            previousZoomLevel = getZoomLevel();
            if (onRegionChangedListener != null) {
                onRegionChangedListener.onRegionEndChange(ExMapView.this, previousMapCenter, previousZoomLevel);
            }
        }
    };

    public ExMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ExMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public ExMapView(Context context, String apiKey) {
        super(context, apiKey);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isTouched = event.getAction() != MotionEvent.ACTION_UP;
        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();

        // If the map region is still changing (user is still scrolling or zooming), reset timer for onRegionEndChange.
        if ((!isTouched && !getMapCenter().equals(previousMapCenter)) || (previousZoomLevel != getZoomLevel())) {

            // If the region has just begun changing, fire off onRegionBeginChange event.
            if (!regionChanging) {
                regionChanging = true;
                if (onRegionChangedListener != null) {
                    onRegionChangedListener.onRegionBeginChange(this, previousMapCenter, previousZoomLevel);
                }
            }

            // Reset timer for onRegionEndChange.
            removeCallbacks(onRegionEndChangeTask);
            postDelayed(onRegionEndChangeTask, changeDuration);
        }
    }

    private void init() {
        changeDuration = DURATION_DEFAULT;
        previousMapCenter = getMapCenter();
        previousZoomLevel = getZoomLevel();
    }

    public void setOnRegionChangedListener(OnRegionChangedListener listener) {
        onRegionChangedListener = listener;
    }

    public void setChangeDuration(int duration) {
        changeDuration = duration;
    }

    public interface OnRegionChangedListener {
        public abstract void onRegionBeginChange(ExMapView exMapView, GeoPoint geoPoint, int zoomLevel);
        public abstract void onRegionEndChange(ExMapView exMapView, GeoPoint geoPoint, int zoomLevel);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜