0

我有以下自定义 MapView,每次缩放地图时都会记录“ZOOMED”。

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import com.google.android.maps.MapView;

public class CustomMapView extends MapView{

    int oldZoomLevel=0;

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

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (getZoomLevel() != oldZoomLevel) {
            Log.i("EOH", "ZOOOMED");

            oldZoomLevel = getZoomLevel();
        }

    }
}

这是我的 onCreate(...) 活动的一个片段:

...
mapView = (CustomMapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
...

现在如何将“ZOOMED”消息添加到我的活动中?!一旦用户缩放,我想刷新地图图标的位置......

我试过看广播接收器,但我不确定这是解决方法吗?

提前谢谢了,

4

1 回答 1

1

您可以在CustomMapView课堂上编写一个简单的侦听器:

public class CustomMapView extends MapView{

    public interface MapZoomListener {

        public void onZoom();
    }

    private MapZoomListener mListener;

    public void setZoomListener(MapZoomListener listener){
        mListener = listener;
    }

    // ... 

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (getZoomLevel() != oldZoomLevel) {
            Log.i("EOH", "ZOOOMED");
            mListener.onZoom();
            oldZoomLevel = getZoomLevel();
        }
    }
}

在您的活动中

...
mapView = (CustomMapView) findViewById(R.id.map);
mapView.setZoomListener(new CustomMapView.MapZoomListener {
    public void onZoom(){
        // Your logic here
    }
});
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
...
于 2012-07-05T12:45:48.647 回答