【发布时间】:2020-10-11 12:26:47
【问题描述】:
我知道关于空白地图存在一些问题,但答案始终是 API 密钥问题。我知道这不是问题,因为如果我只使用标准 MapsActivity 模板并在 Activity 内部运行以下内容,它就可以正常工作:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
而Activity布局如下:
<FrameLayout android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
当我尝试拥有一个通用的宿主 Activity(出于单一 Activity 架构的目的)时会出现问题,这需要我创建一个 Fragment(而不是 Activity)来调用映射逻辑。当我这样做时,我得到一个空白屏幕而不是地图 - 但日志显示 GoogleMap 对象不为空,并且调用了 onMapReady 回调。该应用程序没有崩溃,我没有看到任何启发性的错误。这是我的代码...
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapSubFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapFragment" />
</FrameLayout>
MapFragment.java
public class MapFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
private GoogleMap mMap;
public MapFragment() {
//Not sure why this is necessary, boilerplate from Navigation editor
}
public static MapFragment newInstance() {
//Not sure why this is necessary, boilerplate from Navigation editor
MapFragment fragment = new MapFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapSubFragment);
Log.i("Code", String.valueOf(R.id.mapSubFragment));
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Log.i("Code", String.valueOf(mMap == null));
//Check if user has location permissions granted
setupMap();
}
}
然后是主机 Activity...
NavHostActivity.xml
public class NavHostActivity extends AppCompatActivity {
private NavController navController;
//Methods go here to inflate the toolbar
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_host);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onSupportNavigateUp() {
// Allows NavigationUI to support proper up navigation or the drawer layout
// drawer menu, depending on the situation.
return navController.navigateUp();
}
}
这个宿主活动的布局...
activity_nav_host.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavHostActivity">
//Toolbar stuff goes here
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</RelativeLayout>
最后我的清单包含以下内容:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
感谢您的帮助,我很难过!
【问题讨论】:
-
如果您尝试将单个
Activity与片段一起使用,则不要在Activity布局中嵌入fragment,而是使用FrameLayout和replace或add片段通过FragmentTransaction进行例如:getSupportFragmentManager().beginTransaction().replace(frameLayoutId, new MapFragment()).commit();
标签: java android android-fragments google-maps-android-api-2