android - 在 framelayout 中捏合缩放谷歌地图而不移动标记

标签 android google-maps

我的布局

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <fragment
                android:id="@+id/map"
                class="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

   <ImageView
                android:id="@+id/pin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@null"
                android:src="@drawable/ic_center_pin" />
        </FrameLayout>

下面是我试过的 android How to move a map under a marker

How to attach a flexible marker on map something like Uber and Lyft?

Keep map centered regardless of where you pinch zoom on android

我试过 onCameraChangeListener 方法,下面是代码,但体验很差,

private GoogleMap.OnCameraChangeListener cameraChangedListener = new GoogleMap.OnCameraChangeListener() {
        private float previousZoomValue = 17;
        private CameraUpdate cu;

        @Override
        public void onCameraChange(CameraPosition cameraPosition) {


            if (previousZoomValue == cameraPosition.zoom) {
                mLatitude = cameraPosition.target.latitude;
                mLongitude = cameraPosition.target.longitude;
                cu = CameraUpdateFactory.newLatLng(new LatLng(mLatitude,mLongitude));

            } else if (previousZoomValue > cameraPosition.zoom) {
                previousZoomValue = cameraPosition.zoom;
                mGoogleMap.moveCamera(cu);
            } else {
                mGoogleMap.moveCamera(cu);
                previousZoomValue = cameraPosition.zoom;
            }

        }

我希望当用户点击或捏缩放 map 标记不应该移动时,它应该粘在 map 上

最佳答案

任何依赖于 map 事件的方法都会很慢,所以我建议创建一个助手 View 来管理所有触摸并将其中一些触摸分配到 map (在这种情况下为平移手势) .

布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Helper view to manage touches -->
    <View
        android:id="@+id/helperView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <mypackage.AnchoredImageView
        android:id="@+id/pin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@null"
        android:src="@drawable/ic_center_pin"/>
</FrameLayout>

关于 Activity :

final View mMapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
View mHelperView = findViewById(R.id.helperView);
mHelperView.setOnTouchListener(new View.OnTouchListener() {
    private float scaleFactor = 1f;

    @Override
    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        if (simpleGestureDetector.onTouchEvent(motionEvent)) { // Double tap
            mMap.animateCamera(CameraUpdateFactory.zoomIn()); // Fixed zoom in
        } else if (motionEvent.getPointerCount() == 1) { // Single tap
            mMapView.dispatchTouchEvent(motionEvent); // Propagate the event to the map (Pan)
        } else if (scaleGestureDetector.onTouchEvent(motionEvent)) { // Pinch zoom
            mMap.moveCamera(CameraUpdateFactory.zoomBy( // Zoom the map without panning it
                    (mMap.getCameraPosition().zoom * scaleFactor
                            - mMap.getCameraPosition().zoom) / 5));
        }

        return true; // Consume all the gestures
    }

    // Gesture detector to manage double tap gestures
    private GestureDetector simpleGestureDetector = new GestureDetector(
            MapsActivity.this, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    return true;
                }
            });

    // Gesture detector to manage scale gestures
    private ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(
            MapsActivity.this, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
                @Override
                public boolean onScale(ScaleGestureDetector detector) {
                    scaleFactor = detector.getScaleFactor();
                    return true;
                }
            });
});

由于 ImageView 的 anchor 是图像的中心,如果我们使用像大头针这样的图像,标记显然会移动,因为图像的底部中心必须位于 map 。为避免这种情况,我们可以移动图像以创建锚定 ImageView 。

锚定 ImageView

public class AnchoredImageView extends ImageView {
    public AnchoredImageView(Context context) {
        super(context);
    }

    public AnchoredImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnchoredImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        setTranslationY(-h/2);
    }
}

关于android - 在 framelayout 中捏合缩放谷歌地图而不移动标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157242/

相关文章:

android - 如何在 Android 的 Google map 上解析点并绘制路线?

android - 高程阴影被剪裁

java - Android OpenGL ES 2.0 动态壁纸 - GLSurface 方法未调用

android - kotlin 1.4.30 firebase crashlytics 问题

java - 我如何在一秒内实时获得蓝牙 RSSI 10 次,持续 10 秒

javascript - Google map - 如何使 div 展开以包含其内容

swift - 如何在 iOS swift 中使用自动完成位置功能?

java - Android 应用程序在播放声音时崩溃

ios - GMSMarker iOS 问题

javascript - Google map 信息框位置位于标记旁边而不是位于标记上方