Neranja Gunarathne Neranja Gunarathne - 29 days ago 11
Android Question

getMap() method in new Android API is not working properly for me

Sample code:

SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mMap = mapFragment.getMap();


but it is not working as expected.

Then I replace
getMapAsync(this)method
. Still it´s not working.

Answer

you need to implement OnMapReadyCallback

public class MainActivity extends Activity implements OnMapReadyCallback{

}

so code for onCreate() will be

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);

and overrided method onMapReady() will look like below

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
}