Subclassing of SupportMapFragment in ViewPager wont show map toolbar

731 views Asked by At

Background
I'm implementing a detailed view over a custom location. The activity uses a ViewPager holding different fragments where one should be a SupportMapFragment. I'm testing on a Moto E using 5.0.2.

Problem
When a marker is pressed a map toolbar should slide in from the right. This toolbar does not show. All other controls such as compass and the "pan to my location" button shows. When I do it the normal way with an Activity that has a fragment coded in the XML the controls slide in as they should.

I've also tried adding the fragment to an activity the usual way, like this:

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

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)); } }

Does anyone know what the problem may be? I'm sure it has something to do with my implementation of the SupportMapFragment.

EDIT:
The problem was in the XML of the Activity that is hosting the ViewPager. The MapToolbar is there but off screen. I can barely see it if i rotate the device. If I collapse the appbar before viewing the map tab the MapToolbar is fully visible.
XML Portrait Landscape Collapsed
How can I fix the XML so the MapToolbar is always visible (and enables the collapsing of the toolbar)?

1

There are 1 answers

3
bjiang On

I have done this before, it can popup the MapToolbar in the project, take look at UpcomingFragment here to set up the map.

For the whole project, you can get here to test on. Just go to third tab, and click the marker, it will pop up the MapToolbar behind the red button. In your project, you don't have the red button, so it doesn't matter.

Sample code for setting map:

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();
    }
}

And related XML for this:

<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>