11

ユーザーがマップを移動しているときにトリガーされるイベント ハンドラーの作成に問題があります。ありますが、マップ移動停止後にOnCameraChangeListener発動します。

マップ上で作成しましたが、からのイベントSurfaceViewを処理する方法がわかりません。onScrollOnGestureListener

サンプルコードは次のとおりです。

SurfaceView sv = new SView();
sv.setZOrderOnTop(true);
mapView.addView(sv);

...

public class SView extends SurfaceView implements Runnable, android.view.GestureDetector.OnGestureListener {
...
@Override
public boolean onTouchEvent(MotionEvent event) {
    //this worked in Overlay in Maps API v1
    if(gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return false;
}
...

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.i("test","onScroll");
    return false;
}

...
}

に設定return trueするとonTouchEventonScrollが呼び出されますが、Map を移動できません (これは明らかです。イベントが消費されます)。また、イベントを Map オブジェクトにディスパッチする方法がわかりません。

SurfaceViewマップの移動中に更新する方法を知っている人はいますか?

4

2 に答える 2

15

この問題を解決するために、他のソリューションを実装しました。私がしたこと - GoogleMap をカスタム RelativeLayout 内に配置しました。その後、onInterceptTouchEventコールバックがチャームのように機能し、基になるマップが期待どおりにタッチ イベントを受け取ります。

ここに私のxmlがあります:

<com.my.package.name.map.MapRelativeLayout
    android:id="@+id/map_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</com.my.package.name.map.MapRelativeLayout>

カスタム相対レイアウト:

package com.my.package.name.map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class MapRelativeLayout extends RelativeLayout
{
    private GestureDetector gestureDetector;

    public MapRelativeLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        gestureDetector.onTouchEvent(ev);
        return false;
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY)
        {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY)
        {
            //do whatever you want here
            return false;
        }
    }
}
于 2013-01-09T23:44:51.283 に答える
2

From: Android ListView: オーバーライドするとスクロールが実行されない

メソッドを呼び出してみsuper.onTouchEvent(event)ました onTouchEventか?

編集false- メソッドにも戻りたいと思いますonScroll

次に、onTouchEventメソッドは次のようになります。

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}

その他の役立つリンク:

于 2013-01-09T17:47:03.990 に答える