zoom over specific route google map

前端 未结 9 1076
感动是毒
感动是毒 2021-02-05 11:23

I have a list of random latitude and longitude points and I am drawing a route between them. My question is how to bound this route within google map I made below utili

9条回答
  •  孤城傲影
    2021-02-05 11:51

    The reason, why zooming in is not working might be because map has not been inflated yet at the time of calling the method:

    moveCamera(com.google.android.gms.maps.CameraUpdate)

    Try adding ViewTreeObserver.OnGlobalLayoutListener to the map:

    ViewTreeObserver vto = googleMap.getViewTreeObserver();
    ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                googleMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                googleMap.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
        }
    };
    vto.addOnGlobalLayoutListener(globalLayoutListener);
    

    If the method above does not work GoogleMap has it's own listener for layout, you might use that:

    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    
        @Override
        public void onCameraChange(CameraPosition arg0) {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
            googleMap.setOnCameraChangeListener(null);
        }
    });
    

    However, as of play-services-maps 9.4.0 version of the API the method above is deprecated. Use one of:

    • GoogleMap.OnCameraMoveStartedListener

    • GoogleMap.OnCameraMoveListener

    • GoogleMap.OnCameraIdleListener

提交回复
热议问题