MapFragment return null

前端 未结 6 1090
Happy的楠姐
Happy的楠姐 2020-12-06 07:43
mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doe         


        
6条回答
  •  庸人自扰
    2020-12-06 08:19

    Following the @Glenn-- comment from Oct 1 '13 at 6:05 to create this piece of source code.

    My Implementation is replacing the SupportMapFragment with MapFragment and support Google Maps Version 2

    I want to remember again:

    A GoogleMap can only be acquired using getMap() when the underlying maps system is loaded and the underlying view in the fragment exists. This class automatically initializes the maps system and the view; however you cannot be guaranteed when it will be ready because this depends on the availability of the Google Play services APK. If a GoogleMap is not available, getMap() will return null.

    My Implementation:

    AndroidManifest.xml

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    
    
    
        
        
    
        
        
    
    
    

    activity_main.xml

    
    
        
        
        
    
    

    ApplicationMapFragment.java

    public class ApplicationMapFragment extends MapFragment {
    
        private MapCallback callback;
    
        public void setMapCallback(MapCallback callback) {
            this.callback = callback;
        }
    
        public static interface MapCallback {
            public void onMapReady(GoogleMap map);
        }
    
        @Override 
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            if(callback != null) callback.onMapReady(getMap()); 
        }
    
        /**
         * Initialize default Google Maps Options for our Application
         * @return GoogleMapOptions
         */
        public GoogleMapOptions initializeGoogleMapsOptions() {
            GoogleMapOptions googleMapOptions = new GoogleMapOptions()
                .mapType(GoogleMap.MAP_TYPE_HYBRID);
    
            return googleMapOptions;
        }
    }
    

    MainActivity.java

    public class MainActivity extends Activity implements ApplicationMapFragment.MapCallback {
    
        // Get Class Name
        private static String TAG = MainActivity.class.getName();
    
        // Create a new Map fragment
        private ApplicationMapFragment mapFragment;
    
        // Google Map Fragment Name
        private static String MAP_FRAGMENT_TAG = "google_maps_fragment";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            try {
                    initilizeMapFragment();
            } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, "Google Maps can't be loaded", e);
            }
        }
    
        /**
         * Initialize a new Map Fragment
         */
        private void initilizeMapFragment() {
    
            // Try to get Map Fragment
            mapFragment = (ApplicationMapFragment) getFragmentManager()
                    .findFragmentByTag(MAP_FRAGMENT_TAG);
    
            // We only create a fragment if it doesn't already exist.
            if (mapFragment == null) {
                mapFragment = new ApplicationMapFragment();
                mapFragment.initializeGoogleMapsOptions();
    
                // This activity will receive the Map object once the map fragment is fully loaded
                mapFragment.setMapCallback(this);
    
                // Then we add it using a FragmentTransaction.
                FragmentTransaction fragmentTransaction =
                        getFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.google_map_container, mapFragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
            } else {
                // This activity will receive the Map object once the map fragment is fully loaded
                mapFragment.setMapCallback(this);
            }
        }
    
        @Override
        public void onMapReady(GoogleMap map) {
            Log.d(TAG, "Google Map is loaded");
    
            MarkerOptions marker = new MarkerOptions()
                .position(new LatLng(10, 10))
                .title("Hello World");
    
            map.addMarker(marker);
        }
    }
    

提交回复
热议问题