MapView - Android

2019-05-07 06:39发布

问题:

Hello I'm trying to add MapView to LinearLayout this is my code:

GoogleMap mMap = mMapView.getMap();
if (mMap != null) {
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10);
    if (cameraUpdate != null)
        mMap.moveCamera(cameraUpdate);
    setUpMap(mMap);
}

documantation says; Wait for a GoogleMap to become available from a MapFragment or MapView that you have added to your application. You can verify that the GoogleMap is available by calling the getMap() method and checking that the returned object is not null.

But i still get

java.lang.NullPointerException: CameraUpdateFactory is not initialized.

is there any way to check cameraUpdateFactory is ready?

回答1:

You can force an initialization with the following code

    try {
        MapsInitializer.initialize(context);
    } catch (GooglePlayServicesNotAvailableException impossible) {
        /* Impossible */
    }

Place this snippet in your onCreate method to the top and it should work.