android – 为Google Map v2 Fragment创建OnDragListener

我正在尝试为谷歌地图片段设置onDrag Listener,但无法触发拖动事件.由于地图不直接支持拖动事件,我正在尝试为View实现拖动侦听器.我知道onMarkerDrag事件,但这对我没有好处,因为我不想移动标记.

捕获地图拖动事件的原因有两个:

首先检测用户是否已平移地图.如果他们没有,我喜欢将他们的位置保留在屏幕上,如果它不在地图上,则动画显示他们的位置.如果他们已经平移了地图,我会假设他们知道他们想要看到什么并强制在屏幕上显示位置. (这似乎应该包含在API中)

陷印的第二个原因是允许用户在地图上绘制线条和多边形.我可以使用onTap事件(添加点)来做到这一点,但我也希望能够让它们*地使用它.

我的代码看起来像这样:

主要活动:

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    setUpMapIfNeeded();
    mMapView = findViewById(R.id.map);
    myMapDragListener = new MapDragListener();
    mMapView.setOnDragListener(myMapDragListener);

听众:

public class MapDragListener implements OnDragListener {
    /**
     * 
     */
    public MapDragListener() {
        super();
        say.logCat("MapDragListener: I'm ALIVE!!!!");
    }

    private AkamaiAnnounce say = new AkamaiAnnounce();
    private boolean inDrag;
    private boolean hasPanned=false;

    @Override
    public boolean onDrag(View v, DragEvent event) {
        say.logCat("MapDragListener.onDrag: " + event.toString());
        //String dotTAG = (String) getTag();
        if (event.getLocalState() != this) {
          return false;
        }
        boolean myResult = true;
        int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
          inDrag = true;
          break;
        case DragEvent.ACTION_DRAG_LOCATION:
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
          break;
        case DragEvent.ACTION_DRAG_EXITED:
          break;
        case DragEvent.ACTION_DROP:
          myResult = false;
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          inDrag = false; // change color of original dot back
          hasPanned=true;
          break;
        default:
          myResult = false;
          break;
        }
        return false;
        //return myResult;

    }

    public boolean hasPanned() {
        return hasPanned;
    }

    public void setHasPanned(boolean hasPanned) {
        this.hasPanned = hasPanned;
    }

}

地图工作正常.侦听器实例化,但我的onDrag事件永远不会触发.有任何想法吗?

解决方法:

1)创建包装类:

public class MapWrapperLayout extends FrameLayout {

    public interface OnDragListener {
        public void onDrag(MotionEvent motionEvent);
    }

    private OnDragListener mOnDragListener;

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mOnDragListener != null) {
            mOnDragListener.onDrag(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setOnDragListener(OnDragListener mOnDragListener) {
        this.mOnDragListener = mOnDragListener;
    }
}

2)创建MapFragment类的子类:

public class CustomMapFragment extends SupportMapFragment {

    private View mOriginalView;
    private MapWrapperLayout mMapWrapperLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mOriginalView = super.onCreateView(inflater, container, savedInstanceState);

        mMapWrapperLayout = new MapWrapperLayout(getActivity());
        mMapWrapperLayout.addView(mOriginalView);

        return mMapWrapperLayout;
}

    @Override
    public View getView() {
        return mOriginalView;
    }

    public void setOnDragListener(MapWrapperLayout.OnDragListener onDragListener) {
        mMapWrapperLayout.setOnDragListener(onDragListener);
    }

3.1)最后,在你的活动中:

    // Google map init block
    CustomMapFragment customMapFragment = ((CustomMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
    customMapFragment.setOnDragListener(new MapWrapperLayout.OnDragListener() {
        @Override
        public void onDrag(MotionEvent motionEvent) {
            Log.d("ON_DRAG", String.format("ME: %s", motionEvent));
            // Handle motion event:
        }
    });
    GoogleMap map = customMapFragment.getMap();

3.2)……并在你的布局中:

<fragment android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="your.package.CustomMapFragment"/>
上一篇:javascript-在D3中拖动事件的两个“调用”


下一篇:vue_过渡_动画