有时候需要在地图有缩放的时候动态的显示一些内容,在网上搜索后取得一定的资料

1. 增加接口 interface MapViewListener ;

public interface MapViewListener {
    void onPan(GeoPoint oldTopLeft, GeoPoint oldCenter,
            GeoPoint oldBottomRight, GeoPoint newTopLeft, GeoPoint newCenter,
            GeoPoint newBottomRight); //移动事件,新旧两点的坐标

    void onZoom(GeoPoint oldTopLeft, GeoPoint oldCenter,
            GeoPoint oldBottomRight, GeoPoint newTopLeft, GeoPoint newCenter,
            GeoPoint newBottomRight, int oldZoomLevel, int newZoomLevel);

//缩放事件,新旧点的坐标, 缩放前后的缩放级别

    void onClick(GeoPoint clickedPoint);
}

2. 写一个类继承MapView类

public class ATMapView extends MapView {

// 添加的绽放监听事件
    public interface OnZoomChangeListener {
        public void onZoomChange(MapView view, int newZoom, int oldZoom);
    } 

    //放大接口

    public interface OnPanChangeListener {
        public void onPanChange(MapView view, GeoPoint newCenter,
                GeoPoint oldCenter);
    }

    //移动接口

 

    private GeoPoint mOldTopLeft;
    private GeoPoint mOldCenter;
    private GeoPoint mOldBottomRight;
    private int mOldZoomLevel = -1;

    private MapViewListener mMapViewListener;

    public MapViewListener getMapViewListener() {
        return mMapViewListener;
    }

    public void setMapViewListener(MapViewListener value) {
        mMapViewListener = value;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            GeoPoint newCenter = this.getMapCenter();
            GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
            GeoPoint newBottomRight = this.getProjection().fromPixels(
                    this.getWidth(), this.getHeight());

            if (this.mMapViewListener != null
                    && newTopLeft.getLatitudeE6() == mOldTopLeft
                            .getLatitudeE6()
                    && newTopLeft.getLongitudeE6() == mOldTopLeft
                            .getLongitudeE6()) {
                mMapViewListener.onClick(this.getProjection().fromPixels(
                        (int) ev.getX(), (int) ev.getY()));
            }
        }
        return super.onTouchEvent(ev);
    }

    //容器组件上的绘制

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        GeoPoint newCenter = this.getMapCenter();
        GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
        GeoPoint newBottomRight = this.getProjection().fromPixels(
                this.getWidth(), this.getHeight());
        int newZoomLevel = this.getZoomLevel();

        if (mOldCenter == null)
            mOldCenter = newCenter;

        if (mOldTopLeft == null)
            mOldTopLeft = newTopLeft;

        if (mOldBottomRight == null)
            mOldBottomRight = newBottomRight;

        if (newTopLeft.getLatitudeE6() != mOldTopLeft.getLatitudeE6()
                || newTopLeft.getLongitudeE6() != mOldTopLeft.getLongitudeE6()) {
            if (this.mMapViewListener != null) {
                GeoPoint oldTopLeft, oldCenter, oldBottomRight;

                oldTopLeft = mOldTopLeft;
                oldCenter = mOldCenter;
                oldBottomRight = mOldBottomRight;

                mOldBottomRight = newBottomRight;
                mOldTopLeft = newTopLeft;
                mOldCenter = newCenter;

                mMapViewListener.onPan(oldTopLeft, oldCenter, oldBottomRight,
                        newTopLeft, newCenter, newBottomRight);
            }
        }

        if (mOldZoomLevel == -1)
            mOldZoomLevel = newZoomLevel;
        else if (mOldZoomLevel != newZoomLevel && mMapViewListener != null) {
            int oldZoomLevel = mOldZoomLevel;
            GeoPoint oldTopLeft, oldCenter, oldBottomRight;
            oldTopLeft = mOldTopLeft;
            oldCenter = mOldCenter;
            oldBottomRight = mOldBottomRight;

            mOldZoomLevel = newZoomLevel;
            mOldBottomRight = newBottomRight;
            mOldTopLeft = newTopLeft;
            mOldCenter = newCenter;

            mMapViewListener.onZoom(oldTopLeft, oldCenter, oldBottomRight,
                    newTopLeft, newCenter, newBottomRight, oldZoomLevel,
                    newZoomLevel);
        }
    }

 

3. 第三方 ,在mapview 里设置监听器

mMapView.setMapViewListener(new MapViewListener() {
            @Override
            public void onZoom(GeoPoint oldTopLeft, GeoPoint oldCenter,
                    GeoPoint oldBottomRight, GeoPoint newTopLeft,
                    GeoPoint newCenter, GeoPoint newBottomRight,
                    int oldZoomLevel, int newZoomLevel) {
                // TODO Auto-generated method stub
                computerGeofence();
            }

            @Override
            public void onPan(GeoPoint oldTopLeft, GeoPoint oldCenter,
                    GeoPoint oldBottomRight, GeoPoint newTopLeft,
                    GeoPoint newCenter, GeoPoint newBottomRight) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onClick(GeoPoint clickedPoint) {
                // TODO Auto-generated method stub

            }
        });