1

I had working code before updating my SDK today(for future reference look at the question asked date). .getMap used to give warnings that it was deprecated but now it is not even recognized as valid input. I am assuming this has happened because of the new release of API 24(Android Nougat). Without further ado here's the code:

private boolean initMap() {
        if (mMap == null) {
            MapFragment mapFrag =
                    (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMapAsync();
        }
        return (mMap != null);
    }

If anyone could help me to make changes to integrate .getMapAsync() I would really appreciate it.

Thank you!

UPDATE: Here's the new and updated code after looking at suggestions in answers(this is NOT in the onCreate)-

private boolean initMap() {
        if (mMap == null) {
            MapFragment mapFrag = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
            mapFrag.getMapAsync(this);
        }
            return (mMap != null);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        if (googleMap != null) {
            mMap = googleMap;
        }
    }
petnamedsteve
  • 347
  • 1
  • 2
  • 11
  • Have you tried anything? Can you refer to the documentation and try it, and see what happens? – nasch Aug 01 '16 at 13:42
  • @nasch I have tried to add `(GoogleMaps googlemaps)` in the empty parenthesis, but it didn't work. – petnamedsteve Aug 01 '16 at 13:44
  • Why did you do that? The documentation says to pass in an `OnMapReadyCallback`, not a `GoogleMap`. – nasch Aug 01 '16 at 13:47
  • @nasch I admittedly followed a SO answer, rather than the official documentation(which I have browsed through). – petnamedsteve Aug 01 '16 at 13:49
  • Possible duplicate of [Replace getMap with getMapAsync](http://stackoverflow.com/questions/31371865/replace-getmap-with-getmapasync) – N Dorigatti Aug 01 '16 at 15:13

2 Answers2

1

What you need:

1.compile 'com.google.android.gms:play-services-maps:9.2.0' in your gradle file (latest maps lib), if you can't use it you might need to download the latest tools.

2.The activity that loads the map fragment has to implement OnMapReadyCallback. (This is the easiest way, you can create a different class that implements OnMapReadyCallback).

3.In the activity onCreate method, you need to do the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    /// additiona code here
    MapFragment mapFrag = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFrag.getMapAsync(this); //this is instance of your activity.
    //maybe code here
}

//callback method from OnMapReadyCallback
@Override
public void onMapReady(GoogleMap googleMap) {
    if (googleMap != null) {
        mMap = googleMap;
        //now the map is loaded, you can use it
    }
}

EDIT AFTER CODE EDIT ON SO POST

Your initMap is wrong, it can't return a boolean value because the getMapAsync is as the name says, async, that means the method will be executed and it will return immediately, not waiting for completion, so the return (mMap != null); will almost always return false. You should use onMapReady to check if the map is loaded or not.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • Thanks for answering! Please look at updated code :D – petnamedsteve Aug 02 '16 at 10:49
  • I'm sorry I didn't get you. I have to put the "if map null" stuff in `onMapReady` if I got you correctly. If that is the case, please spoon feed me a give me the exact code because I don't understand how to do that. Thanks! – petnamedsteve Aug 02 '16 at 11:22
1

You need to call like this

    mapFrag = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFrag.getMapAsync(this);

and make your actifity (for instance) implement OnMapReadyCallback. Then override call back method onMapReady(GoogleMap googleMap)