2

我正在研究谷歌地图 v2。

我需要在地图触摸上做一些事情,但我无法获得 Google Maps v2 的触摸监听器。

我正在膨胀包含片段 XML 的 XML。

我的代码如下:

  public class MapViewHelper {

    private FragmentActivity objactivity;
    public GoogleMap myMap=null;


    public MapViewHelper(FragmentActivity objactivity) {
        this.objactivity = objactivity;
    }

    /*
     * @return view of map
     * 
     * @author Sandeep Tiwari
     */
    public View getMapView() {
        View objView = View.inflate(objactivity, R.layout.homemapview, null);
        return objView;
    }

    public GoogleMap getMap()
    {
        if (myMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            myMap = ((SupportMapFragment)this.objactivity.getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
    }
        return myMap;
    }


       public class MySupportMapFragment extends SupportMapFragment {
        public View mOriginalContentView;
        public TouchableWrapper mTouchView;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                Bundle savedInstanceState) {
            mOriginalContentView = super.onCreateView(inflater, parent,
                    savedInstanceState);
            mTouchView = new TouchableWrapper(getActivity());
            mTouchView.addView(mOriginalContentView);
            return mTouchView;
        }

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

  public class TouchableWrapper extends FrameLayout {

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            ((MainActivity) objactivity).MotionDown(ev);
            break;

            case MotionEvent.ACTION_POINTER_DOWN:
                ((MainActivity) objactivity).MotionPointerDown(ev);
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                ((MainActivity)objactivity).MotionUp(ev);
                break;

            case MotionEvent.ACTION_MOVE:
                ((MainActivity)objactivity).ActionMove(ev);
                break;

            }
            return super.dispatchTouchEvent(ev);
        }
    }
}

这是我在其中膨胀 xml 的 java 类:-

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

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

</RelativeLayout>

但是当我触摸地图放大缩小地图时,没有任何动作可以指导我。提前致谢。

4

1 回答 1

2

我将我的 xml 更改为:-

 <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.fd.viewhelper.MapViewHelper"
        />

这里 com.fd.viewhelper.MapViewHelper 是扩展 SupportMapFragment 并覆盖其方法的类名,这里运行该类的代码:-

package com.fd.viewhelper;


public class MapViewHelper extends SupportMapFragment {

    private FragmentActivity objactivity;
    public GoogleMap myMap = null;
    private FlashDeaOverlay itemizedoverlay;
    private List<GroupDealModle> objgroupdeallist;
    private ArrayList<FlashDeaOverlay> objflashdealoverlay = new ArrayList<FlashDeaOverlay>();
    private Bitmap objbitmap;
    private double lat = 0, lng = 0;
    private Context objcontext;

    public MapViewHelper()
    {
    }
    public MapViewHelper(FragmentActivity objactivity) {
        this.objactivity = objactivity;
    }

    /*
     * @return view of map
     * 
     * @author Sandeep Tiwari
     */
    public View getMapView() {
        View objView = View.inflate(objactivity, R.layout.homemapview, null);
        return objView;
    }

    public GoogleMap InstantiateMapView() {
        if (myMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            myMap = ((SupportMapFragment) this.objactivity
                    .getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
        }
        return myMap;
    }



    public View mOriginalContentView;
    public TouchableWrapper mTouchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        mOriginalContentView = super.onCreateView(inflater, parent,
                savedInstanceState);
        mTouchView = new TouchableWrapper(getActivity());
        mTouchView.addView(mOriginalContentView);
        return mTouchView;
    }

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

    public class TouchableWrapper extends FrameLayout {

        public TouchableWrapper(Context context) {
            super(context);
            objcontext = context;
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ((MainActivity) objcontext).MotionDown(ev);
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                ((MainActivity) objcontext).MotionPointerDown(ev);
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                ((MainActivity) objcontext).MotionUp(ev);
                break;

            case MotionEvent.ACTION_MOVE:
                ((MainActivity) objcontext).ActionMove(ev);
                break;

            }
            return super.dispatchTouchEvent(ev);
        }
    }

    // Map Activity must implement this interface
    public interface UpdateMapAfterUserInterection {
        public void onUpdateMapAfterUserInterection();
    }
}
于 2013-04-24T03:24:11.173 回答