Android Google Maps onMapReady store GoogleMap

前端 未结 1 436
别跟我提以往
别跟我提以往 2020-12-18 10:13

i have an android app in development, which uses this google maps api v2. To get an instance of the google map im using the onMapReady callback. In this callback i get an in

相关标签:
1条回答
  • 2020-12-18 10:58

    Yes, you can store an instance of the Google Map reference, and re-use it just as you would if you called getMap() instead of getMapAsync().

    Just make sure to re-call getMapAsync() from onResume() if needed, since often the map reference will become null after onPause() is called.

    Here is a simple example to illustrate. This code places a Marker each time the user taps the map, something you need a valid map reference to do. There is also a button that calls startActivityForResult() and launches another Activity.

    Here is the code:

    public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
    
        private GoogleMap mMap;
        private Marker marker;
        private Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
    
            button = (Button) findViewById(R.id.testButton);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MapsActivity.this, TestActivity.class);
                    startActivityForResult(i, 100);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            if (requestCode == 100) {
                Log.d("MyMap", "onActivityResult " + data.getStringExtra("result"));
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            Log.d("MyMap", "onPause");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            Log.d("MyMap", "onResume");
            setUpMapIfNeeded();
        }
    
        private void setUpMapIfNeeded() {
    
            if (mMap == null) {
    
                Log.d("MyMap", "setUpMapIfNeeded");
                ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMapAsync(this);
            }
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.d("MyMap", "onMapReady");
            mMap = googleMap;
            setUpMap();
        }
    
        private void setUpMap() {
    
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            mMap.getUiSettings().setMapToolbarEnabled(false);
    
    
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    
                @Override
                public void onMapClick(LatLng point) {
    
                    Log.d("MyMap", "MapClick");
    
                    //remove previously placed Marker
                    if (marker != null) {
                        marker.remove();
                    }
    
                    //place marker where user just clicked
                    marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
    
                    Log.d("MyMap", "MapClick After Add Marker");
    
                }
            });
    
        }
    }
    

    Here are the resulting logs from running the app, tapping it once to place a Marker, then clicking the button to launch the second Activity, returning back to the map Activity, and then tapping the map again to place Markers.

    You can see that when onPause() was called after the button click launches the other Activity, the map reference was lost, as when onResume() was called, it made another call to getMapAsync(). However, it's all fine, because the code is set up to take this into account.

     D/MyMap﹕ onResume
     D/MyMap﹕ setUpMapIfNeeded
     D/MyMap﹕ onMapReady
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ onPause
     D/MyMap﹕ onActivityResult ok
     D/MyMap﹕ onResume
     D/MyMap﹕ setUpMapIfNeeded
     D/MyMap﹕ onMapReady
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
     D/MyMap﹕ MapClick
     D/MyMap﹕ MapClick After Add Marker
    
    0 讨论(0)
提交回复
热议问题