gpt4 book ai didi

android - 如何在googleMap android中检测onDoubleTap缩放

转载 作者:行者123 更新时间:2023-11-30 00:06:38 26 4
gpt4 key购买 nike

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

最佳答案

您可以在 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 - 更好的方法是创建自定义 View ,它扩展 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:

Activity 的 Activity 布局 (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);
}
}

关于android - 如何在googleMap android中检测onDoubleTap缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com