Android地图:如何确定地图中心一拖完成后地图、一拖、完成后、中心

2023-09-04 03:58:22 作者:难得爱浓

有没有通过Android的地图API,在那里我可以检测出地图中心平移后完成动画的方式?我想用这个信息来从服务器动态加载标记。 谢谢 BD

Is there a way through the android maps API, where I can detect the map center after pan animation has completed? I want to use this information to load markers from a server dynamically. Thanks BD

推荐答案

我也一直在寻找一个做了结尾拖解决方案,检测在地图移动结束后,正好在那一刻地图中心。我还没有找到它,所以我做了这个简单的实现细做的工作:

I also have been looking for a "did end drag" solution that detects the map center at the moment exactly after the map ended moving. I haven't found it, so I've made this simple implementation that did work fine:

private class MyMapView extends MapView {

    private GeoPoint lastMapCenter;
    private boolean isTouchEnded;
    private boolean isFirstComputeScroll;

    public MyMapView(Context context, String apiKey) {
        super(context, apiKey);
        this.lastMapCenter = new GeoPoint(0, 0);
        this.isTouchEnded = false;
        this.isFirstComputeScroll = true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
            this.isTouchEnded = false;
        else if (event.getAction() == MotionEvent.ACTION_UP)
            this.isTouchEnded = true;
        else if (event.getAction() == MotionEvent.ACTION_MOVE)
            this.isFirstComputeScroll = true;
        return super.onTouchEvent(event);
    }
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (this.isTouchEnded &&
            this.lastMapCenter.equals(this.getMapCenter()) &&
            this.isFirstComputeScroll) {
            // here you use this.getMapCenter() (e.g. call onEndDrag method)
            this.isFirstComputeScroll = false;
        }
        else
            this.lastMapCenter = this.getMapCenter();
    }
}

就这样,我希望它能帮助! O /

That's it, I hope it helps! o/