Android Google Maps onMapReady store GoogleMap

旧街凉风 提交于 2019-11-29 07:53:13

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!