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
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