2013-04-23 76 views
2

我正在使用Google Maps v2。爲什麼觸摸不適用於谷歌地圖V2?

我需要在地圖觸摸上做一些操作,但我無法獲得Google地圖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); 
     } 
    } 
} 

這是我的Java類中,我膨脹的xml: -

<?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> 

但是當我觸摸地圖zoomin縮小地圖不採取行動執行任何一個引導我。 在此先感謝。

回答

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(); 
    } 
}