Neranja Gunarathne Neranja Gunarathne - 1 month ago 13
Android Question

how can fixed getMap() method in new APIs in android?this method has a error now

please help me.my sample code is

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


but it's not working.then i replace getMapAsync(this)method.also but not working my code.please tell me a solution.
thank you.

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;
}