GoogleMap.setOnMapLoadedCallback 未被调用

GoogleMap.setOnMapLoadedCallback is not called

我想使用 google地图和地理编码功能获取位置。放置找到我正确的纬度、经度、格式化地址。 onMapReady 函数被调用但 onMapLoaded 函数根本没有被调用。

清单:

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

我的 google gms 版本:

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'

我的自定义地图视图布局:

<?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());

    }

}

试试这段代码,你还没有调用 MapView 的正确生命周期方法。 docs

中已经解释过了

你自定义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());
}

你的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);
    }
}