0

I am using clustering for maker representation on map. But when user click on any cluster item. It bounds all latlng within cluster. this is my code:

 @Override
public boolean onClusterClick(Cluster<MapMakerModel> cluster) {
    String firstName = cluster.getItems().iterator().next().getTitle();
    showSnackbar(cluster.getSize() + " (including " + firstName + ")");

    // Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
    // inside of bounds, then animate to center of the bounds.

    // Create the builder to collect all essential cluster items for the bounds.
    LatLngBounds.Builder builder = LatLngBounds.builder();
    for (ClusterItem item : cluster.getItems()) {
        builder.include(item.getPosition());
    }
    // Get the LatLngBounds
    final LatLngBounds bounds = builder.build();


    // Animate camera to the bounds
    try {
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

so when this code is called it bound all latlng within that cluster.that is right but it also rotate camera from user angle to NS angle which I don't want to do. How I can archive this??

Deepak
  • 127
  • 4
  • 11

2 Answers2

2

Returned by CameraUpdateFactory.newLatLngBounds() method CameraUpdate object has

a bearing of 0 and a tilt of 0.

But you can get current (before set to bounds) bearing (probably tilt too) and set it AFTER camera animation. And not exactly after if you set duration of bounding box zoom animation to 0 and do all animation in next steps. Something like that:

...
// Get the LatLngBounds
final LatLngBounds bounds = builder.build();

// Get bearing and tilt
final float bearing = mGoogleMap.getCameraPosition().bearing;
final float tilt = mGoogleMap.getCameraPosition().tilt;

// Animate camera to the bounds
try {
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100) ,0 /*duration of "set to bounds" animation */, new GoogleMap.CancelableCallback(){
    @Override
    public void onCancel() {
    }

    @Override
    public void onFinish() {
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
            .bearing(bearing)
            .bearing(tilt)
            .build()), 1000 /*duration of "set bearing and tilt" animation */, null);
    }
});
} catch (Exception e) {
    e.printStackTrace();
}
...

Or you need to calculate your cluster LatLngBounds manually and use CameraUpdateFactory.newCameraPosition() instead of CameraUpdateFactory.newLatLngBounds().

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
0

It looks like this bug still persists but I found a solution here: CameraPosition doesn't work well with setPadding

Magic happens when I set the padding left/right and top/bottom to the same value and it works just like expected. +1px for one of the sides and the drift ist around +- 20 meters, +100px and the drift is out of map.

cpro90
  • 176
  • 10