2013-06-24 29 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))); 
     } 
} 
相关问题