How to use a Google Map in a NavigationDrawer?

前端 未结 1 1367
青春惊慌失措
青春惊慌失措 2020-12-22 03:42

I am relatively new to Coding but please bear with me. I am trying to put a Google Map in a navigation drawer. I figured the best way is to use fragments but despite my vari

相关标签:
1条回答
  • 2020-12-22 03:53

    You can't do a FragmentTransaction with an Activity.

    In order to use a Google Map in a NavigationDrawer, use a Fragment that extends SupportMapFragment, and add all of the functionality that your MapsActivity class currently has.

    Use this to start with:

    public class MapsFragment extends SupportMapFragment
            implements OnMapReadyCallback {
    
        GoogleMap mGoogleMap;
    
        @Override
        public void onResume() {
            super.onResume();
    
            setUpMapIfNeeded();
        }
    
        private void setUpMapIfNeeded() {
    
            if (mGoogleMap == null) {
                getMapAsync(this);
            }
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap)
        {
            mGoogleMap=googleMap;
            mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        }
    
    }
    

    Then in the Activity, change this to use the Fragment:

    // Set to start with fragment
    MapsFragment fragment = new MapsFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
    
    0 讨论(0)
提交回复
热议问题