我已经使用相同的代码和实现过程在两个片段中实现了SupportMapFragment。一个片段被加载到滚动视图中。突然,此片段在其上显示黑屏。仅显示“ Google”一词。但是另一个片段运作良好。我已在清单元数据中添加了api键,并且还在值中添加了google_maps_api.xml。仍然有黑屏。请帮忙。
Xml代码:
<LinearLayout
android:id="@+id/google_map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:visibility="gone">
<fragment
android:id="@+id/map"
class="util.WorkaroundMapFragment"
android:layout_width="@dimen/localization_map_width"
android:layout_height="@dimen/localization_map_height">
</fragment>
</LinearLayout>
Java代码
GoogleMap mMap;
WorkaroundMapFragment mapFragment;
解决方法图片段:
public class WorkaroundMapFragment extends SupportMapFragment {
private OnTouchListener mListener;
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);
TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return layout;
}
public void setListener(OnTouchListener listener) {
mListener = listener;
}
public interface OnTouchListener {
public abstract void onTouch();
}
public class TouchableWrapper extends FrameLayout {
public TouchableWrapper(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mListener.onTouch();
break;
case MotionEvent.ACTION_UP:
mListener.onTouch();
break;
}
return super.dispatchTouchEvent(event);
}
}
}
初始化地图
if (mapFragment == null) {
mapFragment = (WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
}
mapFragment.setListener(() -> scrollView.requestDisallowInterceptTouchEvent(true));
if (mMap == null) {
mapFragment.getMapAsync(this);
} else {
settingMapOnLatLng();
}
地图初始化
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
settingMapOnLatLng();
}