Reputation: 2458
I have an onClusterItemClickListener. I want the google map (v2) to animate to that position so I use CameraUpdateFactory.newCameraPosition.
Problem: google maps animates to the target LatLng but does not zoom, tilt, etc. I troubleshooted with GoogleMap.CancelableCallback() and it seems that after the animation to the position onCancel() is always called.
Here is my code
...<some code before>
@Override
public boolean onClusterItemClick(OffersMarker item) {
final LatLng markerLocation = item.getPosition();
final OffersMarker i = item;
final CameraPosition MARKER_POS = new CameraPosition.Builder()
.target(markerLocation)
.zoom(22)
.bearing(0)
.tilt(25)
.build();
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(MARKER_POS));
}
This code only animates to target with no zoom or tilt.
Here is the version two with troubleshooting
@Override
public boolean onClusterItemClick(OffersMarker item) {
final LatLng markerLocation = item.getPosition();
final OffersMarker i = item;
final CameraPosition MARKER_POS = new CameraPosition.Builder()
.target(markerLocation)
.zoom(22)
.bearing(0)
.tilt(25)
.build();
changeCamera(gmap, CameraUpdateFactory.newCameraPosition(MARKER_POS), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
Log.d("CAM_MOVE", "Finished animate camera");
}
@Override
public void onCancel() {
Log.d("CAM_MOVE", "Canceled animate camera");
}
});
}
Here is change camera method
private void changeCamera(GoogleMap map, CameraUpdate cameraUpdate, GoogleMap.CancelableCallback cancelableCallback) {
map.animateCamera(cameraUpdate, cancelableCallback);
}
onCancel() is allways called by unknown source.
I am not sure if those are connected (onCancel() being called and zoom not working) but It might as well be. Any help much appreciated.
Upvotes: 1
Views: 3078
Reputation: 2458
So the solution was hiding in the click listener returning false and thus the default method went into action.
Solutions: return true after the onClick code.
Example
private void addClusterItemClickListener(GoogleMap map, ClusterManager cM){
final GoogleMap gmapI = map;
cM.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<OffersMarker>() {
@Override
public boolean onClusterItemClick(OffersMarker item) {
Log.d("CAM_MOVE", "Clicked on marker");
final LatLng markerLocation = item.getPosition();
final OffersMarker i = item;
if (markerLocation != null) {
//Log.d("GEO_CODE_CRASH", "Latitude: " + markerLocation.latitude + " | " + "Longitude: " + markerLocation.longitude);
//Log.d("CAM_MOVE", "Moving on with placeSelectHandler");
placeSelectedHandler(markerLocation.latitude, markerLocation.longitude);
buildingFragment.setNrOfOffersOnToolber(i.getNrOfOffers());
getServerResponseAndPopulateBuildingPage(adr,markerLocation);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(markerLocation, ZOOM_LEVEL_BUILDING);
gmap.animateCamera(update);
}
return true; //<------- THIS NEEDS TO BE TRUE
}
});
}
Upvotes: 5