安卓地图:如何确定拖动完成后的地图中心

11

在Android Maps API中,是否有一种方法可以在平移动画完成后检测地图中心?我想利用这些信息从服务器动态加载标记。

感谢您,BD

3个回答

12

我也一直在寻找一个“拖动结束”解决方案,可以在地图停止移动的那一刻准确检测地图中心。 我还没有找到这个解决方案,所以我做了这个简单的实现,它运作良好:

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/


3

您是否正在使用MapActivity? 这是我使用的代码:

MapView mapView = (MapView)findViewById(R.id.map);
Projection projection = mapView.getProjection();
int y = mapView.getHeight() / 2; 
int x = mapView.getWidth() / 2;

GeoPoint geoPoint = projection.fromPixels(x, y);
double centerLatitude = (double)geoPoint.getLatitudeE6() / (double)1E6;
double centerLongitude = (double)geoPoint.getLongitudeE6() / (double)1E6;

你需要添加类似于以下代码的内容:

你确实需要添加类似于以下代码的内容:

@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
    boolean result = super.dispatchTouchEvent(event);
    if (event.getAction() == MotionEvent.ACTION_UP)
        reload_map_data();    ///  call the first block of code here
    return result;
}

这个处理程序能够处理地图在你松开手指后稍微移动的“fling”吗? - Jeremy Logan
嗨,感谢您的回复marcc,但我需要找到一种方法来检测pan动画何时停止,以便在那个点使用您的中心计算代码。 - vamsibm

0

自2016年8月起,Android地图可以检测到事件,例如onCameraMoveStarted(以及移动的原因,例如REASON_GESTUREREASON_DEVELOPER_ANIMATION)。

以下代码(大部分取自docs)给出了您可以实现的一些想法:

public class MyCameraActivity extends FragmentActivity implements
        OnCameraMoveStartedListener,
        OnCameraMoveListener,
        OnCameraMoveCanceledListener,
        OnCameraIdleListener,
        OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_camera);

        SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;

        mMap.setOnCameraIdleListener(this);
        mMap.setOnCameraMoveStartedListener(this);
        mMap.setOnCameraMoveListener(this);
        mMap.setOnCameraMoveCanceledListener(this);

        // Show Sydney on the map.
        mMap.moveCamera(CameraUpdateFactory
                .newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
    }

    @Override
    public void onCameraMoveStarted(int reason) {

        if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
            Toast.makeText(this, "The user gestured on the map.",
                           Toast.LENGTH_SHORT).show();
        } else if (reason == OnCameraMoveStartedListener
                                .REASON_API_ANIMATION) {
            Toast.makeText(this, "The user tapped something on the map.",
                           Toast.LENGTH_SHORT).show();
        } else if (reason == OnCameraMoveStartedListener
                                .REASON_DEVELOPER_ANIMATION) {
            Toast.makeText(this, "The app moved the camera.",
                           Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCameraMove() {
        Toast.makeText(this, "The camera is moving.",
                       Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCameraMoveCanceled() {
        Toast.makeText(this, "Camera movement canceled.",
                       Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCameraIdle() {
        Toast.makeText(this, "The camera has stopped moving.",
                       Toast.LENGTH_SHORT).show();
        // Here you get the camera center
        GeoPoint geoPoint = projection.fromPixels(mMap.getHeight() / 2, mMap.getWidth() / 2);
        double centerLat = (double)geoPoint.getLatitudeE6() / (double)1E6;
        double centerLng = (double)geoPoint.getLongitudeE6() / (double)1E6;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接