MapView添加Overlay后没有滚动

发布于 2024-12-15 18:47:11 字数 2223 浏览 0 评论 0原文

我现在正在与以下问题作斗争: 我在地图视图中添加了一个简单的叠加层。它不会绘制任何内容,它只是一个用于检测滚动结束的帮助叠加层(请参阅 响应

这是它的代码:

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

如果我将此叠加层添加到我的地图视图中,我不能不再滚动地图。下面是从地图视图添加和删除覆盖层的代码:

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

我错了什么?

我注意到另一件事。覆盖层的绘制方法将被调用、调用、调用,而无需用户执行任何操作。有什么原因吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-12-22 18:47:11

您将在叠加层的触摸事件上返回 true。当您返回 true 时,触摸事件将停止级联到较低的视图。因此,您的 MapView 永远不会收到它。仅当触摸已完全处理并且您不想再执行任何操作时才需要执行此操作。如果你想让地图移动,可以尝试将return语句改为return super.onTouchEvent(e, mapView);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文