There is no scroll after adding an Overlay to MapV

2019-05-24 11:55发布

问题:

I'm fighting with following problem now: I add a simple overlay to my mapview. It doesn't draw anything, it is just a help overlay for detecting scroll is ended (see response)

Here is the code of it:

public class ScrollDetectionOverlay extends Overlay
{
    private static GeoPoint lastLatLon = new GeoPoint(0, 0);
    private static GeoPoint currLatLon;

    protected boolean isMapMoving = false;

    private MapScrollingFinishedListener listener = null;

    public void setListener(MapScrollingFinishedListener listener)
    {
        this.listener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView)
    {
        super.onTouchEvent(e, mapView);
        if (e.getAction() == MotionEvent.ACTION_UP)
        {
            isMapMoving = true;
        }
        return true;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
        super.draw(canvas, mapView, shadow);
        if (!shadow)
        {
            if (isMapMoving)
            {
                currLatLon = mapView.getProjection().fromPixels(0, 0);
                if (currLatLon.equals(lastLatLon))
                {
                    isMapMoving = false;
                    listener.onScrollingFinished();
                }
                else
                {
                    lastLatLon = currLatLon;
                }
            }
        }
    }
}

If I'm adding this overlay to my mapview, I can't scroll the map anymore. Below is the code for adding and removing of the overlay from the mapview:

@Override
protected void onResume()
{
    super.onResume();
    if (!getMapView().getOverlays().contains(scrollDetectionOverlay))
    {
        scrollDetectionOverlay = new ScrollDetectionOverlay();
        scrollDetectionOverlay.setListener(this);
        getMapView().getOverlays().add(scrollDetectionOverlay);
    }
}

@Override
protected void onPause()
{
    super.onPause();
    if (getMapView().getOverlays().contains(scrollDetectionOverlay))
    {
        getMapView().getOverlays().remove(scrollDetectionOverlay);
        scrollDetectionOverlay = null;
    }
}

What do I'm wrong?

And another thing I noticed. The draw-method of overlay will be called, and called, and called, without user does anything. Some reason for it?

Thank you.

回答1:

You're returning true on the touch event for the overlay. When you return true, the touch event stops cascading down to the lower views. Thus, your MapView never receives it. You only want to do that if the touch has been completely handled and you don't want to do anything more. If you want the map to move, you may try changing the return statement to return super.onTouchEvent(e, mapView);