如何在googleMap android中检测onDoubleTap放大

时间:2018-02-19 05:38:39

标签: android google-maps

我正在尝试在anroid中检测googleMap上的onDoubleTap缩放(仅缩放既不滚动也不旋转/倾斜)。实际上我想实现uber使用的缩放概念,如果你在地图上双击标记就是在放大地图之前放置的位置稳定,我的意思是坐标应该是相同的,无论什么是缩放级别。例如,如果我在地图上的任何地方双击,它应该总是放大到mapView的中心。

1 个答案:

答案 0 :(得分:1)

您可以修改前一个问题的that答案中的public boolean dispatchTouchEvent(MotionEvent event),以检测第一次和第二次点击之间延迟的双击,例如:

public class TouchableWrapper extends FrameLayout {

    private GoogleMap mGoogleMap = null;
    private long mLastTouchTime = -1;      // time of first tap

    public TouchableWrapper(Context context) {
        super(context);
    }

    public void setGoogleMap(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);

                long thisTime = System.currentTimeMillis();  // time of second tap tap
                if (thisTime - mLastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
                    // double tap detected! do you magic here:
                    if (mGoogleMap != null) {
                        LatLng zoomCenter = mGoogleMap.getProjection().fromScreenLocation(new Point((int)event.getX(), (int)event.getY()));
                                                    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(zoomCenter, currentZoom + 1));
                    }
                    mLastTouchTime = -1;
                } else {
                    mLastTouchTime = thisTime;
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                }

            break;

            case MotionEvent.ACTION_POINTER_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(false);
            break;

            case MotionEvent.ACTION_POINTER_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            break;

            case MotionEvent.ACTION_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            break;
        }

        return super.dispatchTouchEvent(event);
    }
}

<强>更新

如果您使用MapView而不是MapFregment - 更好的方法是创建自定义视图,扩展MapView实现OnMapReadyCallback(这是获取GoogleMap的必要条件对象)和覆盖dispatchTouchEvent()(用于双击检测)。这样的事情:

public class EnhanchedMapView extends MapView implements OnMapReadyCallback {
    private long mLastTouchTime = -1;

    private OnMapReadyCallback mMapReadyCallback;
    private GoogleMap mGoogleMap;
    private Marker mMarker;
    private Paint mPaintArrow;

    public EnhanchedMapView(@NonNull Context context) {
        super(context);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable GoogleMapOptions options) {
        super(context, options);
        init();
    }

    private void init() {
    }

    @Override
    public void getMapAsync(OnMapReadyCallback callback) {
        mMapReadyCallback = callback;
        super.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);

                long thisTime = System.currentTimeMillis();
                if (thisTime - mLastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {

                    if (mGoogleMap != null) {
                        LatLng zoomCenter = mGoogleMap.getProjection().fromScreenLocation(new Point((int) event.getX(), (int) event.getY()));
                        float currentZoom = mGoogleMap.getCameraPosition().zoom;
                        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(zoomCenter, currentZoom + 1));
                    }
                    mLastTouchTime = -1;
                } else {
                    mLastTouchTime = thisTime;
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                }

                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(false);
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
                break;

            case MotionEvent.ACTION_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
                break;
        }

        return super.dispatchTouchEvent(event);
    }
}

更新#2:

活动的活动布局(MainActivity),案例可以是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="{YOUR_PACKAGE}.MainActivity">

    <{YOUR_PACKAGE}.EnhanchedMapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</RelativeLayout>

只有MainActivity源代码中的更改为:

...
private EnhanchedMapView mMapView;
...

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

    ...
    mMapView = (EnhanchedMapView) findViewById(R.id.mapview);
    ...
}

更新#3:

public class EnhanchedMapView extends MapView implements OnMapReadyCallback {
    private long mLastTouchTime = -1;

    private OnMapReadyCallback mMapReadyCallback;
    private GoogleMap mGoogleMap;

    private LatLng mZoomCenter;

    public EnhanchedMapView(@NonNull Context context) {
        super(context);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public EnhanchedMapView(@NonNull Context context, @Nullable GoogleMapOptions options) {
        super(context, options);
        init();
    }

    private void init() {
    }

    @Override
    public void getMapAsync(OnMapReadyCallback callback) {
        mMapReadyCallback = callback;
        super.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
                mGoogleMap.getUiSettings().setZoomGesturesEnabled(false);

                mZoomCenter = mGoogleMap.getCameraPosition().target;

                long thisTime = System.currentTimeMillis();
                if (thisTime - mLastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {

                    if (mGoogleMap != null) {
                        LatLng zoomCenter = mGoogleMap.getProjection().fromScreenLocation(new Point((int) event.getX(), (int) event.getY()));
                        float currentZoom = mGoogleMap.getCameraPosition().zoom;
                        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mZoomCenter, currentZoom + 1));
                    }
                    mLastTouchTime = -1;
                } else {
                    mLastTouchTime = thisTime;
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                }

                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(false);
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
                break;

            case MotionEvent.ACTION_UP:
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
                break;
        }

        return super.dispatchTouchEvent(event);
    }
}