谷歌地图:视图大小太小异常

时间:2016-05-05 21:17:48

标签: android google-maps

在我的Android应用程序中,我有一个显示地图片段列表的Activity,以下是代码:

public class TTMapFragment extends TTFragment {

private final static String LOG_AREA = "TTMapFragment";

private SupportMapFragment mMapFragment;
private Bundle mBundle;
protected int leftPosition;
protected int topPosition;
private GoogleMap googleMap;

public GoogleMap getMap() {
    if (googleMap == null) {
        final CountDownLatch latch = new CountDownLatch(1);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getSupportMapFragment().getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        TTMapFragment.this.googleMap = googleMap;
                        latch.countDown();
                    }
                });
            }
        });
        try {
            latch.await(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {}
    }
    return googleMap;
}

public int getMapViewResourceID(return R.id.trip_small_map;);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View inflatedView = super.onCreateView(inflater, container, savedInstanceState);

    MapsInitializer.initialize(getActivity());

    setUpMapIfNeeded();

    mMapFragment = getSupportMapFragment();
    mMapFragment.onCreate(mBundle);
    mMapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            TTMapFragment.this.googleMap = googleMap;
            int width = 0;
            int height = 0;
            if (mMapFragment.getView() != null) {
                width = mMapFragment.getView().getWidth();
                height = mMapFragment.getView().getHeight();
            }
            // code to get data for map and add markers on the map
            //Then I call this
            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bound, width, height, padding);
            try {
                getMap().moveCamera(cu);
            } catch (IllegalStateException e) {
                // here IO get this exception : view size too small
                TTLog.d(LOG_AREA, "handled view size too small issue");
            }
        }
    });

    return inflatedView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBundle = savedInstanceState;
}

public SupportMapFragment getSupportMapFragment() {
    setUpMapIfNeeded();
    return mMapFragment;
}

private void setUpMapIfNeeded() {
    if (mMapFragment == null) {
        mMapFragment = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(getMapViewResourceID()));
    }
}

public void clearMap(){
    getMap().clear();
    System.gc();
    TTLog.d(LOG_AREA, "clearMap done");
}

@Override
public void onDestroy() {
    TTLog.d(LOG_AREA, "onDestroy");
    clearMap();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    System.gc();
}
}

当我致电getMap().moveCamera(cu)时,我得到IllegalStateException: View size too small after padding。任何人都可以建议我何时可以调用moveCamera()函数?

我观察到的另一件事是,第一个片段的宽度和高度为零,但之后它给出了非零宽度和高度。

0 个答案:

没有答案