ViewPager中的SupportMapFragment子类化不会显示地图工具栏

时间:2015-06-15 22:29:37

标签: android google-maps android-fragments supportmapfragment

背景
我正在实现自定义位置的详细视图。该活动使用一个ViewPager来保存不同的片段,其中一个应该是一个SupportMapFragment。我正在使用5.0.2在Moto E上进行测试。

问题
按下标记后,地图工具栏应从右侧滑入。此工具栏不显示。所有其他控件,如指南针和“平移到我的位置”按钮显示。当我使用具有在XML中编码的片段的Activity的正常方式时,控件将按其应用的方式滑入。

我也尝试过以通常的方式将片段添加到活动中,如下所示:
    

Fragment fragment = MapFragment.newInstance(title, lat, lng);
getSupportFragmentManager().beginTransaction().replace(R.id.testMapContainer, fragment).commit();
实施

public class MapFragment extends SupportMapFragment implements OnMapReadyCallback {

private final static String TITLE_KEY = "titleKey"; private final static String LATITUDE_KEY = "latitudeKey"; private final static String LONGITUDE_KEY = "longitudeKey"; private final static float ZOOM = 14; private String title; private double latitude; private double longitude; public MapFragment() { super(); } public static MapFragment newInstance(String title, double latitude, double longitude) { MapFragment fragment = new MapFragment(); Bundle bundle = new Bundle(); bundle.putString(TITLE_KEY, title); bundle.putDouble(LATITUDE_KEY, latitude); bundle.putDouble(LONGITUDE_KEY, longitude); fragment.setArguments(bundle); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = super.onCreateView(inflater, container, savedInstanceState); Bundle bundle = getArguments(); title = bundle.getString(TITLE_KEY); latitude = bundle.getDouble(LATITUDE_KEY); longitude = bundle.getDouble(LONGITUDE_KEY); getMapAsync(this); return v; } @Override public void onMapReady(GoogleMap map) { LatLng targetLatLng = new LatLng(latitude, longitude); map.getUiSettings().setMapToolbarEnabled(true); map.setBuildingsEnabled(true); map.setMyLocationEnabled(true); map.moveCamera(CameraUpdateFactory.newLatLngZoom(targetLatLng, ZOOM)); map.addMarker(new MarkerOptions() .title(title) .position(targetLatLng)); } }

有谁知道问题可能是什么?我确定它与我的SupportMapFragment的实现有关。

修改
问题出在托管ViewPager的Activity的XML中。 MapToolbar不在屏幕上。如果我旋转设备,我几乎看不到它。如果我在查看地图选项卡之前折叠了appbar,则MapToolbar完全可见 XML Portrait Landscape Collapsed
如何修复XML以使MapToolbar始终可见(并启用工具栏的折叠)?

1 个答案:

答案 0 :(得分:0)

之前我已完成此操作,它可以弹出项目中的MapToolbar,查看UpcomingFragment here来设置地图。

对于整个项目,您可以here进行测试。只需转到third tabclick the marker,它会弹出红色按钮后面的MapToolbar。在你的项目中,你没有红色按钮,所以它并不重要。

设置地图的示例代码:

public class UpcomingFragment extends Fragment {

    private SupportMapFragment mMapView;

    public static UpcomingFragment newInstance(String param1, String param2) {
        UpcomingFragment fragment = new UpcomingFragment();
        return fragment;
    }

    MapView mapView;
    GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_upcoming, container, false);
        // Gets the MapView from the XML layout and creates it

        MapsInitializer.initialize(getActivity());


        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                mapView = (MapView) v.findViewById(R.id.map);
                mapView.onCreate(savedInstanceState);
                // Gets to GoogleMap from the MapView and does initialization stuff
                if (mapView != null) {
                    map = mapView.getMap();
                    map.getUiSettings().setMyLocationButtonEnabled(false);
                    map.setMyLocationEnabled(true);
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
                    map.animateCamera(cameraUpdate);
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
                Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
        }

        // Updates the location and zoom of the MapView

        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));

        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
}

相关的XML:

<RelativeLayout 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"
        android:name="com.bojie.materialtest.fragments.UpcomingFragment"/>

</RelativeLayout>