您可以在 Fragment(或 Activity)中使用MapView,这将允许您使用所需的任何布局。
即您的布局可能如下所示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
您还需要将 Fragment 的生命周期方法(例如 onCreate、onResume 等)转发到 MapView。
唯一的区别(似乎是 Google Maps 中的错误?)是您还需要手动初始化 Google Maps:
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = mMapView.getMap();
if (mMap != null) {
// Thought official docs says that it is not necessary to call
// initialize() method if we got not-null GoogleMap instance from
// getMap() method it seems to be wrong in case of MapView class.
try {
MapsInitializer.initialize(getActivity());
setUpMap(mMap);
} catch (GooglePlayServicesNotAvailableException impossible) {
mMap = null;
}
}
}
}