MapView - Android

爷,独闯天下 提交于 2019-12-07 10:15:58

问题


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.



来源:https://stackoverflow.com/questions/13802644/mapview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!