我需要动态地将地图添加到我的MainActivity的应用程序中,否则我会因内存泄漏而受到影响。添加地图基本上有效,但我无法添加标记。它们不会出现。这就是我添加地图的方式:
MainActivity 使用的<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Placeholder for all fragments -->
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<!-- Slider menu -->
<LinearLayout
android:id="@+id/list_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical" >
<!-- more code -->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_googlemap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.androidmapsextensions.SupportMapFragment"
map:cameraTargetLat="48.21167860510698"
map:cameraTargetLng="16.365892895859423"
map:cameraZoom="11"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true" />
</FrameLayout>
public class MapFragment extends com.androidmapsextensions.SupportMapFragment
{
private static final String TAG = MapFragment.class.getSimpleName();
private OnFragmentLoadedCompleteListener onFragmentLoadedCompleteListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args)
{
super.onCreateView(inflater, container, args);
Logger.log(TAG, "onCreateView()", LogController.isLoggingEnabled(), Log.DEBUG);
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Logger.log(TAG, "onActivityCreated()", LogController.isLoggingEnabled(), Log.DEBUG);
onFragmentLoadedCompleteListener.onFragmentComplete();
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
Logger.log(TAG, "onAttach()", LogController.isLoggingEnabled(), Log.DEBUG);
onFragmentLoadedCompleteListener = (OnFragmentLoadedCompleteListener)activity;
}
}
@Override
public void onFragmentComplete()
{
googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentByTag("mapFragment")).getExtendedMap();
}
private void initMapFragment()
{
if (googleMap != null)
{
googleMap.clear();
googleMap = null;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment(), "mapFragment").commit();
}
public interface OnFragmentLoadedCompleteListener
{
public void onFragmentComplete();
}
执行onFragmentComplete(),googleMap不为null并显示。但我不能在上面画上标记。它们不会出现。
但是,当我不动态添加地图并将其嵌入RelativeLayout中的activity_main.xml时,就像这样
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_googlemap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.androidmapsextensions.SupportMapFragment"
map:cameraTargetLat="48.21167860510698"
map:cameraTargetLng="16.365892895859423"
map:cameraZoom="11"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true" />
</RelativeLayout>
并且在没有监听器的情况下获取这样的地图,我可以绘制标记,这意味着它们会出现。
googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_googlemap)).getExtendedMap();
这意味着只有当我将地图直接嵌入到MainActivity的xml中而不是将其作为片段动态添加时,地图上的绘图标记才有效。这是怎么回事。错误在哪里?
答案 0 :(得分:0)
通常情况下,我自己找到了解决方案。
我必须保留对我用来替换的MapFragment的引用:
mapFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit();
在onFragmentComplete()中,我必须在此引用上调用 getChildFragmentManager()以获取&#34;更正&#34;谷歌地图。
googleMap = ((SupportMapFragment)mapFragment.getChildFragmentManager().findFragmentById(R.id.fragment_googlemap)).getExtendedMap();
这次也可以通过id找到它(之前没有工作),并且此地图上的绘图标记现在可以正常工作。