捏缩放谷歌地图,在framelayout中移出标记

Nag*_*aga 3 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>
Run Code Online (Sandbox Code Playgroud)

下面的东西我试过 android如何在标记下移动地图

如何在Uber和Lyft等地图上附上灵活的标记?

无论你在哪里捏缩放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;
            }

        }
Run Code Online (Sandbox Code Playgroud)

我想当用户点击或捏缩放地图标记不应该移动时,它应该坚持地图

ant*_*nio 10

任何依赖于地图事件的方法都是滞后的,所以我建议创建一个View管理所有触摸的辅助工具,并将其中的一些调度发送到地图(在这种情况下平移手势).

布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

在活动上:

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;
                }
            });
});
Run Code Online (Sandbox Code Playgroud)

由于a的锚点ImageView是图像的中心,如果我们使用像图钉一样的图像,标记将显然移动,因为图像的底部中心必须位于地图的中心.为避免这种情况,我们可以移动图像来创建锚定图像视图.

AnchoredImageView

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);
    }
}
Run Code Online (Sandbox Code Playgroud)