Android Google Maps onMapReady store GoogleMap

2019-01-25 21:44发布

问题:

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 instance of the google map, can i store this instace local an reuse it, without getting a new one every time i have to update the map? Or is there any issue with reusing this instance over time? I just like to get sure that there is nothing with which i could get into problem.

Thanks for your answers!

回答1:

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