【发布时间】:2017-03-05 05:26:06
【问题描述】:
我一直在搜索很多关于将地图片段添加到活动中并执行片段事务以添加和删除地图片段就像普通片段一样。我在 stackoverflow 上发现了很多较旧的帖子,但其中大多数使用 getMap() 方法来获取googleMap 对象,但当前版本的地图没有 getMap() 方法,但它有 getMapAsync() 所以我找到的解决方案不是我正在寻找的解决方案。
我也发现有人用这个
GoogleMap map=((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap() 但这里 getMap() 在当前版本的地图中也未定义。
所以任何人都可以给我一个适当的指导来实现。
这是代码,我想如何实现:
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>
所以我想做的就是像普通片段一样使用片段事务将此片段添加到另一个活动中。
【问题讨论】:
-
你可以把这个地图片段放在一个片段中,然后用这个片段替换活动
-
你能举个例子吗?
-
当然,等一下。
标签: java android google-maps android-layout android-fragments