I'm using the new Android maps V2 with this layout:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraBearing="270"/>
I'm trying to use the method newLatLngBounds (LatLngBounds bounds, int padding) for zooming and seeing all my markers in the map, however the camera bearing is set to 0.
The description on google developers documentation says:
public static CameraUpdate newLatLngBounds (LatLngBounds bounds, int padding) (...) . The returned CameraUpdate has a bearing of 0 and a tilt of 0. (...)".
How can I change the bearing value?
I tried to set a new bearing programmatically after the call of newLatLngBounds, something like this:
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(mMap.getCameraPosition().target)
.zoom(mMap.getCameraPosition().zoom)
.bearing(270)
.build()));
But when I do this some markers won't show up.
bad issue on Google's CameraUpdateFactory i solved it with time, tiny timespan!
looks dirty, but works
3 Handler do the job, every postdelay must be greater than the one before
I've got an alternative, I had the same issue. What I'll show is how to convert a bounds to a zoom, and then we'll simply use a CameraPosition.builder() to create the right position.
Then you can use this code and add a bearing/and or tilt with the bounds:
This will work (you'll have to change mMap to your actually map variable and the id to your map id).
Good luck! I got the boundZoomLevel from post: How do I determine the zoom level of a LatLngBounds before using map.fitBounds? Go like that answer too if you have time!
yes you can! just use: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera(com.google.android.gms.maps.CameraUpdate, int, com.google.android.gms.maps.GoogleMap.CancelableCallback)
and implement a CancelableCallback and put in the onFinish() method: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.CancelableCallback#onFinish()
the code to change the bearing. Of course you will not get a single animation but a chain of two, but it will work!
I don't think you need target and zoom, i kept them commented out since it should be managed by the previous animation
EDIT Looking better at the API, I have a method that could work (I can't test it for now, please take a look at it: having a LatLngBounds you can get what is the zoom level which best fits the area (it depends on the device dimensions, so you have to get view dimensions at runtime), and then, when you have the zoom (you have also the center of the LatLngBounds), you can do a camera update with one unique animation: https://developers.google.com/android/reference/com/google/android/gms/maps/model/CameraPosition, the constructor allows the set of the values, or you can use the builder as previously seen.
You can find online various ways to do that, for example (just a quick search online obtained a javascript version https://stackoverflow.com/a/13274361/4120431 )
Hope this can help you with your issue!