androiddictionarylocationandroid-mapview

GoogleMap.setOnMapLoadedCallback is not called


I want to get location with using googleMap and geocode features. Place find my right latitude,longitude, formatted address. onMapReady function is called but onMapLoaded function is not called at all.

Manifest:

<meta-data
 android:name="com.google.android.geo.API_KEY"
 android:value="..." />

My google gms versions:

implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-gcm:15.0.1'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-places:15.0.1'

My customMapView layout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <com.google.android.gms.maps.MapView
       android:id="@+id/map_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

    <TextView
       android:id="@+id/address_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0"
       android:ellipsize="end"
       android:maxLines="1"
       android:padding="5dp"
       android:singleLine="true"
       android:visibility="visible"
       tools:text="21 Jump St, Atlanta GA 30311" />
</merge>

CustomMapView.java:

    public class CustomMapView extends LinearLayout {

    private MapView mapView;
    private TextView textView;

    public CustomMapView(Context context) {
        this(context, null);
    }

    public CustomMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public CustomMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.map_view, this, true);

        this.mapView = ViewUtil.findById(this, R.id.map_view);
        this.textView = ViewUtil.findById(this, R.id.address_view);
    }

    public void display(Place place) {

        this.mapView.onCreate(null);
        this.mapView.onResume();

        this.mapView.setVisibility(View.VISIBLE);
        this.imageView.setVisibility(View.GONE);

        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final @Nonnull GoogleMap googleMap) {

                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
                googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
                googleMap.setBuildingsEnabled(true);
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                googleMap.getUiSettings().setAllGesturesEnabled(false);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));

                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {

                        googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap bitmap) {

                               // it is not called
                            }
                        });
                    }
                });
            }
        });

        this.textView.setText(place.getDescription());

    }

}

Solution

  • Try this code, You haven't called right lifecycle methods of the MapView. It's already explained in the docs

    You custom View:

    public class CustomMapView extends LinearLayout {
    
    private MapView mapView;
    private TextView textView;
    
    public CustomMapView(Context context) {
        this(context, null);
    }
    
    public CustomMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }
    
    public void onCreate(Bundle var1) {
        mapView.onCreate(var1);
    }
    
    public void onResume() {
        mapView.onResume();
    }
    
    public void onPause() {
        mapView.onPause();
    }
    
    public void onStart() {
        mapView.onStart();
    }
    
    public void onStop() {
        mapView.onStop();
    }
    
    public void onDestroy() {
        mapView.onDestroy();
    }
    
    public void onLowMemory() {
        mapView.onLowMemory();
    }
    
    public void onSaveInstanceState(Bundle var1) {
        mapView.onSaveInstanceState(var1);
    }
    
    private void initialize(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.map_view, this, true);
    
        this.mapView = ViewUtil.findById(this, R.id.map_view);
        this.textView = ViewUtil.findById(this, R.id.address_view);
    }
    
    public void display(Place place) {
        this.mapView.setVisibility(View.VISIBLE);
        this.imageView.setVisibility(View.GONE);
    
        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final @Nonnull GoogleMap googleMap) {
    
                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
                googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
                googleMap.setBuildingsEnabled(true);
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                googleMap.getUiSettings().setAllGesturesEnabled(false);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));
    
                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
    
                        googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap bitmap) {
    
                                // it is not called
                            }
                        });
                    }
                });
            }
        });
    
        this.textView.setText(place.getDescription());
    }
    

    Your Activity

    public class MainActivity extends Activity {
        private CustomMapView customView;
    
        @Override
        public void onCreate(Bundle var1) {
            super.onCreate(var1);
    
            customView = ViewUtil.findById(this, R.id.custom_view_id);
            customView.onCreate(var1);
        }
    
        @Override
        public void onResume() {
            super.onResume();
            customView.onResume();
        }
    
        @Override
        public void onPause() {
            super.onPause();
            customView.onPause();
        }
    
        @Override
        public void onStart() {
            super.onStart();
            customView.onStart();
        }
    
        @Override
        public void onStop() {
            super.onStop();
            customView.onStop();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            customView.onDestroy();
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            customView.onLowMemory();
        }
    
        @Override
        public void onSaveInstanceState(Bundle var1) {
            super.onSaveInstanceState(var1);
            customView.onSaveInstanceState(var1);
        }
    }