【问题标题】:Dynamically adding Map Fragment into an activity using FragmentManager and FragmentTransaction使用 FragmentManager 和 FragmentTransaction 将地图片段动态添加到活动中
【发布时间】: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


【解决方案1】:

将此地图片段添加到一个片段(MapFragment)布局文件中:

     <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

公开声明mapview和googlemap:

private MapView mapView;
private GoogleMap googleMap;

在 onViewCreated() 中添加以下代码:

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);

在您的片段中实现 OnMapReadyCallback 并添加此方法:

@Override
public void onMapReady(GoogleMap map) {
    googleMap = map;
}

在activity_main.xml中添加一个容器:

     <FrameLayout
        android:id="+@id/container"
        android:layout_width = "match_parent" 
        android:layout_height = "match_parent">

       // Add all activity contents here

     <FrameLayout/>

用 MapFragment 替换这个容器:

FragmentManager fm = getFragmentManager():
fm.beginTransaction.replace(R.id.container, new MapFragment()).commit(); 

希望这会有所帮助。

【讨论】:

  • 它不工作。它是说不兼容的类型“无法将 Fragment 转换为 GoogleMap”对象和 getMap() 方法也不可用,但 getMapAsync() 存在。
  • 我改变了我的答案。现在您不必使用 getMap()。它也已被弃用。而是使用 getMapAsync。此外,您不应该在片段中调用片段。请改用地图视图。我在很大程度上改变了我的答案。请检查更新的答案。
  • 是的,就像魅力一样。感谢 tahsinRupam 的帮助和时间。
猜你喜欢
  • 2015-01-24
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
相关资源
最近更新 更多