2014-11-24 57 views
3
public class Trees extends FragmentActivity implements GoogleMap.OnMarkerClickListener { 

    private GoogleMap mMap; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_trees); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMap(); 
    } 

    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
      mMap.setMyLocationEnabled(true); 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude),17)); 
     } 
    } 
} 
+2

那麼這樣做有什麼問題?你有沒有嘗試過目前? – 2014-11-24 03:59:15

+0

我已經嘗試過,實際上我正在實現帶有碎片的導航抽屜,並且沒有在地圖活動中獲取導航抽屜,因爲它是一個碎片活動。請儘快幫我解決這個問題? – user3550704 2014-11-25 00:27:26

回答

0

首先,將您的FragmentActivity更改爲片段。另外,因爲MapFragment也是一個片段,所以你現在將嵌套Fragments。有很多解決方案/黑客可以使用嵌套的Fragment(對於你的情況,它是Fragment中的MapFragment)。但最推薦的一個是動態添加MapFragment到您的Fragemnt。爲此,你必須遵循這些簡單的步驟。

首先,你應該簡單地擁有的FrameLayout在mapfragment.xml這樣

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android 
    android:id="@+id/fl_map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
</FrameLayout> 

現在你必須動態地添加SupportMapFragment在您的片段

private SupportMapFragment fragment; 

裏面你onActivityCreated(),複製此代碼

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    FragmentManager fm = getChildFragmentManager(); 

    if (fragment == null) { 
     fragment = SupportMapFragment.newInstance(); 
     fm.beginTransaction().replace(R.id.fl_map, fragment).commit(); 
    } 
} 
片段的3210

因爲所以你必須初始化地圖在的onResume()這樣

@Override 
public void onResume() { 
    super.onResume(); 
if (mMap == null) 
     mMap = fragment.getMap(); 
    try { 
     if (mMap != null) { 
      builder = new LatLngBounds.Builder(); 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      mMap.getUiSettings().setZoomControlsEnabled(false); 
      mMap.getUiSettings().setZoomGesturesEnabled(true); 
      mMap.setMyLocationEnabled(true); 

      // do whatever you want to do with map 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

也不要忘了在onDetach(添加該代碼FragmentTransaction將需要一些時間)

@Override 
public void onDetach() { 
    super.onDetach(); 
    try { 
     Field childFragmentManager = Fragment.class 
       .getDeclaredField("mChildFragmentManager"); 
     childFragmentManager.setAccessible(true); 
     childFragmentManager.set(this, null); 

    } catch (NoSuchFieldException e) { 
     throw new RuntimeException(e); 
    } catch (IllegalAccessException e) { 
     throw new RuntimeException(e); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

希望這會幫助你。