On zoom event for google maps on android

前端 未结 11 2016
清酒与你
清酒与你 2020-11-30 00:04

We\'re building an application which is using the google maps api for android.

I have my MapController and MapView, and I enable the built-in zoom controls using:

11条回答
  •  爱一瞬间的悲伤
    2020-11-30 00:26

    For pre-V2.0, I made a class which extends MapView that alerts a listener with events when the map region starts changing (onRegionBeginChange) and stops changing (onRegionEndChange).

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

提交回复
热议问题