如何检测Android MapView是否已被平移或缩放?

时间:2010-02-24 18:54:20

标签: android android-mapview

我正在创建一个Android应用,根据MapView的可见区域搜索项目。有没有办法在我的MapView上设置一个侦听器来检测地图被平移或缩放的时间?

6 个答案:

答案 0 :(得分:4)

尝试mapview-overlay-manager,它是Android地图覆盖层的扩展,

它有一些简化的OnGestureListener,很少有例子:

onSingleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onDoubleTap(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onLongPress(MotionEvent, ManagedOverlay, GeoPoint, OverlayItem)

onZoom(ZoomEvent, ManagedOverlay)

onScrolled(...)

链接:http://code.google.com/p/mapview-overlay-manager/希望有所帮助

答案 1 :(得分:3)

您可以创建一个扩展MapView的SimpleMapView。

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

你有听众可以把代码放到平移或缩放。 然后在你的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>

然后在您的代码中,您可以指定Pan Listener:

mapView.addPanChangeListener(new PanChangeListener() {

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

答案 2 :(得分:1)

可悲的是,在MapView工具中没有内置功能(这是一个奇怪的疏忽,因为此功能在JavaScript SDK以及iOS SDK中)。

通过使用Runnable并只轮询MapView,您可以轻松地处理这个问题。我这样做是为了跟踪“最后”状态:

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

然后将它们与当前值进行比较。如果更改的值,您知道地图视图已移动。如果没有,你什么都不做。

无论哪种方式,在500ms左右后重新安排runnable进行另一次运行并重复该过程。您可以使用onResume()和onPause()来删除Runnable的回调并根据需要重新启动它。

答案 3 :(得分:1)

MapView类可以使用onLayout方法跟踪更改。

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

答案 4 :(得分:0)

我能想到的唯一方法是扩展MapView并覆盖OnTouchEvent并观察Up操作。这将告诉您用户已完成移动,您可以获得纬度/经度范围来确定您应该检查的区域。

答案 5 :(得分:0)

老问题,但我写了一个叫做ExMapView的类,它在地图区域开始变化时(onRegionBeginChange)和停止变化时触发事件(onRegionEndChange)。此类用于旧版MapView,而不是V2.0。希望它可以帮到某人。

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