getMapAsync() is not giving callback to onMapReady() to initialize googleMap object

110
January 13, 2018, at 09:23 AM

I have been working on google maps and below is the method I am using to initialize the map. Surprisingly, mapFragment.getMapAsync() method is not getting executed so googleMaps object is still null after calling getMapAsync().

// Initialize the google map
void initMap() {
    Log.d(MAPSACTIVITY_TAG, "Initializing google map");
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.maps_fragment);
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap gMap) {
            Log.d(MAPSACTIVITY_TAG,"Maps is ready");
            googleMap=gMap;
        }
    });
    Log.d(MAPSACTIVITY_TAG,"Value of google maps is "+googleMap);
    //This will get device location, set GoogleAPIClient and set mundu marker
    if (isLocationPermissionGranted) {
        getDeviceLocation();
        buildGoogleApiClient();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(context, "Sorry! App requires location permission", Toast.LENGTH_SHORT).show();
            return;
        }
        googleMap.setMyLocationEnabled(true);
        //this will get client's location and set client's marker
        geoLocateClient();
    } else
        Log.d(MAPSACTIVITY_TAG, "Location permission is not given");
}

Here is the Logcat snapshot:

Please notice, Log statement in the getAsync is never executed. Can anyone help me with this?

Answer 1

You should move code that uses variable googleMap inside onMapReady(...) { ... } method because getMapAsync(...) is asynchronous and onMapReady(...) will be called later in a different thread after Maps has responded.

READ ALSO
Switching to first tab in tabbed activity not working properly in android

Switching to first tab in tabbed activity not working properly in android

So I have a weird problemBasically I have a Main activity with four buttons, each button opens a different tab (there is 4 tabs)

116
How do we show a notification in the OS with React Native and Android?

How do we show a notification in the OS with React Native and Android?

So I just set up a Firebase function to send notifications to my devices and I added react-native-firebase to my project so I signin, get a token and receive notifications

136
Android 8.0 Oreo RunningServiceInfo returns 0 services

Android 8.0 Oreo RunningServiceInfo returns 0 services

my Oneplus 5 just updated to Oreo and something has brokenI have a simple widget which starts and stops some service

185
In App purchase in Libgdx [on hold]

In App purchase in Libgdx [on hold]

I am planing to integrate in-app purchase in my Libgdx game but unfortunately Libgdx does not have a good (in-app purchasing) implementation tutorial on their Github wiki and the only source is to look into https://githubcom/libgdx/gdx-pay so I copy-pasted...

151