Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapView - Android

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?

like image 505
mavixce Avatar asked Dec 20 '22 12:12

mavixce


1 Answers

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.

like image 127
Greeny Avatar answered Dec 31 '22 10:12

Greeny