Skip to content Skip to sidebar Skip to footer

Android Google Maps Onmapready Store Googlemap

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

Solution 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:

publicclassMapsActivityextendsAppCompatActivityimplementsOnMapReadyCallback {

    privateGoogleMap mMap;
    privateMarker marker;
    privateButton button;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        button = (Button) findViewById(R.id.testButton);

        button.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                Intent i = newIntent(MapsActivity.this, TestActivity.class);
                startActivityForResult(i, 100);
            }
        });
    }

    @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 100) {
            Log.d("MyMap", "onActivityResult " + data.getStringExtra("result"));
        }
    }

    @OverrideprotectedvoidonPause() {
        super.onPause();

        Log.d("MyMap", "onPause");
    }

    @OverrideprotectedvoidonResume() {
        super.onResume();

        Log.d("MyMap", "onResume");
        setUpMapIfNeeded();
    }

    privatevoidsetUpMapIfNeeded() {

        if (mMap == null) {

            Log.d("MyMap", "setUpMapIfNeeded");
            ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMapAsync(this);
        }
    }

    @OverridepublicvoidonMapReady(GoogleMap googleMap) {
        Log.d("MyMap", "onMapReady");
        mMap = googleMap;
        setUpMap();
    }

    privatevoidsetUpMap() {

        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);


        mMap.setOnMapClickListener(newGoogleMap.OnMapClickListener() {

            @OverridepublicvoidonMapClick(LatLng point) {

                Log.d("MyMap", "MapClick");

                //remove previously placed Markerif (marker != null) {
                    marker.remove();
                }

                //place marker where user just clicked
                marker = mMap.addMarker(newMarkerOptions().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

Post a Comment for "Android Google Maps Onmapready Store Googlemap"