android 地图:拖动完成后如何确定地图中心

发布于 2024-08-12 17:59:40 字数 71 浏览 6 评论 0原文

有没有办法通过 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

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

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

发布评论

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

评论(3

淡忘如思 2024-08-19 17:59:40

我还一直在寻找一种“结束拖动”解决方案,该解决方案可以在地图结束移动后的那一刻检测地图中心。我还没有找到它,所以我做了这个简单的实现,效果很好:

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

就是这样,我希望它有帮助!哦/

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

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

夜灵血窟げ 2024-08-19 17:59:40

您使用的是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;
}

Are you using a MapActivity? Here's the code I've used:

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;

You do need to add code similar to this also:

@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;
}
放飞的风筝 2024-08-19 17:59:40

8 月起,Android 版地图能够检测 onCameraMoveStarted 等事件(以及移动的原因,例如 REASON_GESTUREREASON_DEVELOPER_ANIMATION

自 2016年 以下代码(主要取自文档)给出了以下内容你可以实现什么:

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

Since August 2016 Maps for Android is capable of detecting events such as onCameraMoveStarted (and the reasons for the move, e.g., REASON_GESTURE, REASON_DEVELOPER_ANIMATION.

The following code (mostly taken from the docs) gives an idea of what you can achieve:

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