2013-06-24 31 views
0

我使用FragmentActivty(public class MyGoogleMap extends FragmentActivity { },現在我想用片段設置谷歌地圖設置了谷歌地圖。(public class MyGoogleMap extends Fragment { })。是否有可能,如果是的話,如果你有任何想法比請與我分享,謝謝。如何使用Fragment在android中設置Google Map?

+0

請參閱https://developers.google.com/maps/documentation/android/ –

+0

是的,這是非常可能的。除了@DixitPatel鏈接的文檔中提供的內容之外,沒有太多要添加。只需按照那裏的步驟。 – Rarw

+0

@DixitPatel我在嘗試並感謝您的回答 –

回答

1

創建地圖的框架,其將在XML佈局中添加

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/map_container"> 

<!-- The map fragments will go here --> 
</RelativeLayout> 

不包括無論是在你的片段類XML類=「com.google.android.gms.maps.SupportMapFragment」嗎手動獲取它onActivityCreated

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    FragmentManager fm = getChildFragmentManager(); 
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container); 
    if (fragment == null) { 
     fragment = SupportMapFragment.newInstance(); 
     fm.beginTransaction().replace(R.id.map_container, fragment).commit(); 
    } 


/***at this time google play services is not initialized so get map and add what ever you want to it in onResume() or onStart() **/ 
} 

@Override 
    public void onResume() { 
     super.onResume(); 
     if (map == null) { 
      map = fragment.getMap(); 
      map.addMarker(new MarkerOptions().position(new LatLng(0, 0))); 
     } 
} 
相關問題