Google Maps CameraUpdateFactory not initialized

前端 未结 5 1621
忘掉有多难
忘掉有多难 2021-01-07 18:12

I\'m getting some reports back from user experience through crashlytics giving me an error

Fatal Exception java.lang.NullPointerException         
CameraU         


        
5条回答
  •  攒了一身酷
    2021-01-07 18:21

    Something from my MapUtil, that handles this case:

    public static void animateCamera(final MapView mapView, final GoogleMap map, final LatLng location) {
            try{
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, getBetterZoom(map));
                map.animateCamera(cameraUpdate);
            }catch(Exception e) {
                MapsInitializer.initialize(mapView.getContext());
                if (mapView.getViewTreeObserver().isAlive()) {
                    mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                        @SuppressLint("NewApi")
                        @Override
                        public void onGlobalLayout() {
                            GlobalyLayoutListenerUtil.removeGlobalLayoutListener(mapView, this);
                            animateCamera(mapView, mapView.getMap(), location);
                        }
                    });
                } else {
                    if(map != null){
                        map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                            @Override
                            public void onMapLoaded() {
                                if(location != null){
                                    animateCamera(mapView, mapView.getMap(), location);
                                }
                            }
                        });
                    }
                }
            }
    
        }
    

提交回复
热议问题