我不确定我是否完全理解您的问题,但我有一个类似的设置,我正在使用导航下拉菜单。这对我有用:
1.) 从 xml 文件加载片段并调用 setupMapIfNeeded()
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.basicMap);
setUpMapIfNeeded();
这是供参考的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/basicMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
2.) 然后设置地图(有关 isGoogleMapsInstalled() 的详细信息,请参阅此问题)
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap();
// Check if we were successful in obtaining the map.
if(isGoogleMapsInstalled())
{
if (mMap != null)
{
mMap.setOnCameraChangeListener(getCameraChangeListener());
mMap.setInfoWindowAdapter(new MyCustomInfoWindowAdapter(this));
}
}
else
{
MapConstants.showDialogWithTextAndButton(this, R.string.installGoogleMaps, R.string.install, false, getGoogleMapsListener());
}
}
}
3.) 确保您还从 onResume() 调用 setUpMapIfNeeded():
public void onResume()
{
super.onResume();
setUpMapIfNeeded();
}