我一直在寻找很多关于添加地图片段到一个活动和执行片段事务添加和删除地图片段就像普通片段。我发现很多旧的帖子在stackoverflow但其中大多数用户使用getMap()方法获取googleMap对象,但当前版本的map没有getMap()方法,但它具有getMapAsync(),所以我找到的解决方案并非我寻找的解决方案。 我发现有人使用这也 GoogleMap map=((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap()
但这里还getMap()未定义在当前版本的地图。所以任何人都可以请给我一个适当的指导来实现。 下面是代码,我要怎么实现这一点:使用FragmentManager和FragmentTransaction动态地添加地图片段到一个活动
public class MainActivity extends Fragment{
GoogleMap map;
MapView v;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_main,container,false);
return view;
}
}
哪里activity_main是包含地图片段的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kamaloli.mapdemo.MainActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kamaloli.mapdemo.MainActivity" />
</RelativeLayout>
因此,所有我想要做的是我想使用Fragment事务将此片段添加到另一个活动中,如正常的片段换货。
你可以把这个地图片段放在一个片段中,然后用这个片段替换活动 – tahsinRupam
你可以举个例子吗? –
当然,等一下。 – tahsinRupam