zoom over specific route google map zoom over specific route google map android android

zoom over specific route google map


Try implementing this way

/** * Zooms a Route (given a List of LalLng) at the greatest possible zoom level. * * @param googleMap: instance of GoogleMap * @param lstLatLngRoute: list of LatLng forming Route */public void zoomRoute(GoogleMap googleMap, List<LatLng> lstLatLngRoute) {    if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();    for (LatLng latLngPoint : lstLatLngRoute)        boundsBuilder.include(latLngPoint);    int routePadding = 100;    LatLngBounds latLngBounds = boundsBuilder.build();    googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, routePadding));}

UPDATE

After looking at the image, there are layouts above map. So it would require you to set Variable Padding. You can do it as

googleMap.setPadding(left, top, right, bottom);

Note: While setting variable padding, you can initialize routePadding = 0;In your case, approximations are: left = right = 10, bottom=100, top=200. Once set, you can calibrate'em as per your requirement.

Recommendation: You can calculate the height of those (top and bottom) layouts in pixels and then set padding accordingly.


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:


You need to include all the points that form the polyline into your mapBounds object.

Also, you are updating the camera using animateCamera and then you are moving it using moveCamera. This second camera update overrides the first one. Just remove the line

googleMap.moveCamera(CameraUpdateFactory.zoomOut());

This line is also innecesary (you don't need to move and animate the camera, just one of them):

googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));

If you need to zoom out a bit your map you can play with the padding parameter of the CameraUpdateFactory.newLatLngBounds method.