Force animateCamera to complete animation - android

I'm trying to force the camera to animate to a zoom level and coordinate that describes as precisely as possible the area occupied by a list of LatLng coordinates.
So far, I have the following:
private void focusListOfCoords(Collection<LatLng> coords) {
LatLngBounds.Builder coordsBoundsBuilder = new LatLngBounds.Builder();
for (LatLng coord : coords) {
coordsBoundsBuilder.include(coord);
}
LatLngBounds coordsBounds = coordsBoundsBuilder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(coordsBounds, 50);
mMap.animateCamera(cameraUpdate);
}
The code above, when zoomed all the way out on the whole world simply pans the view slightly to center on the center point of the bounds. It does not zoom in.
If I replace mMap.animateCamera(cameraUpdate) with mMap.moveCamera(cameraUpdate), the projection changes the zoom level (too far in every case, but that's beside the point) and centers as expected.
After trying to troubleshoot this, I noticed that if I implement the CancelableCallback for the animateCamera method, I can see that the animation is being cancelled.
Is there a way to force the animation to finish? If not, how can I hunt down the cause of the animation being cancelled?
Edit: The solution can be found in this question: Android- animateCamera with CameraUpdateFactory.newCameraPosition does NOT zoom
To summarize, my listener method that was calling the animation was returning false which cancelled the animation.

You can try to use a different approach to your implementation.
One is passing CameraUpdateFactory.newLatLngZoom(latlng, zoom) to animateCamera. You can specify the zoom level using this. More info on that here.
Or you can build your CameraView first before passing it to animateCamera. You can check out the docs here.
Not really sure why or how it is trigerring onCancel(). Maybe something is being fired right after your animateCamera call. Try to use the duration parameter to alter the speed of the animation and check for changes.

Related

Is there a way to animate the HERE Android SDK marker movement on the map?

I am trying to animate MapMarker object movement between two point on the map.
This is line that sets new position but without animation.
marker.setCoordinate(new GeoCoordinate(lat, lon));
I found this https://developers.google.com/maps/documentation/javascript/reference/marker?hl=en#Marker.setAnimation but it looks like continuous animation
Maybe you can create manually your own animation with PropertyAnimator or something else and calculating progress of changing position

Change Google Map Offset Center

I'm trying to set the user location on the map such that it sits about 1/3 of the way up from the bottom of the screen, and when the map rotates, it will rotate around this point.
The closest I've got to achieving this is by using the setPadding() method, however, this causes the map to sort of shake when rotated, as the center point sort of 'floats' around where it actually should be. It looks quite ugly
int mapHeight = mapView.getHeight();
googleMap.setPadding(0, mapHeight / 5, 0, 0);
Is there a better way to do this?
Edit: Explained in picture below
You don't need paddings
Change mappoint X and Y values as you need you can call this where you want! may be inside your onLocationChanged like changeOffsetCenter(location.getLatitude(),location.getLongitude());
public void changeOffsetCenter(double latitude,double longitude) {
Point mappoint = mGoogleMap.getProjection().toScreenLocation(new LatLng(latitude, longitude));
mappoint.set(mappoint.x, mappoint.y-100); // change these values as you need , just hard coded a value if you want you can give it based on a ratio like using DisplayMetrics as well
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(mGoogleMap.getProjection().fromScreenLocation(mappoint)));
}
output :
Google map v3 beta seems to fix it, using padding now properly move pivotX/pivotY for rotation
https://developers.google.com/maps/documentation/android-sdk/v3-client-migration
Accepted answer might be work of "animateCamera". Its makes "shaking of rotations" almost invisible. So more easy will be using
mMap.setPadding(leftPx, topPx, rightPx, bottomPx)
with mMap.animateCamera
or twice mMap.moveCamera(in case of changing bearing
1nd moveCamera rotate with center of device, 2nd moveCamera make correct center using padding)
Its kind of hack, while google do not fix rotation considering setPadding

Android Google Map CameraUpdateFactory.newLatLngZoom doesn't work

I'm working on an Android app that records on a Google Map the path a user follows while traveling. As the user moves, I collect the location data into an sqlite database and update a LatLngBounds and an ArrayList holding all the locations making up the path. The camera view for the map can be set to be centered so as to encompass the entire path, centered on the beginning of the path, or centered on the end of the path. When the camera view changes from one setting to another, I'd like to animate the camera. That's where I'm having a problem.
When I switch to take in the whole path, camera animation works as I intend with the following line of code:
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatLngBounds, 110);
However, trying to zoom to the starting point or the ending`point doesn't work. When I switch to centering over the starting or ending point (latLng), I want the camera to zoom to the zoom level that was used when the camera was last centered there, but with a minimum zoom level of 17.0f. Here are the different ways I've tried:
float zoom = 17.0f;
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
float zoom = 17.0f;
CameraPosition cameraPosition = CameraPosition.fromLatLngZoom(latLng, zoom);
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
float zoom = 17.0f;
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
None of these approaches worked. In every instance the camera centered on the designated LatLng, but the zoom level on the map remained where it was immediately before trying to animate the camera. For what it's worth, substituting moveCamera() for animateCamera() worked as I expected in all the variants listed above, with the camera centered over the designated LatLng and (if the prior zoom level was < 17.0f) zoomed to 17.0f. Does anyone have suggestions for what else to try to make the zoom feature work with animateCamera()? Thanks!

tricky Android Google Map zoom while maintaining center point

It's not easy explaining my problem but I will try.
I have an android GoogleMap, on top of it, I have an ImageView positioned at its center at all times. If I drag/pan the map, the pin will always be in the center of the GoogleMap.
Now, I add a marker, somewhere on the map. I want to zoom such that the center point remains in the center of the map, and the marker is visible within the map, and to the highest zoom level.
The problem is if I simply check if the marker is within boundaries of the map or not, and then keep zooming in/out till it is, this process will always repeat itself, i.e. trying to zoom in and if the marker became outside, then zoom out.
The problem is I rely on an OnCameraChange listener which will keep calling itself everytime I zoom in or out, hence, the process of zooming in/out will keep occuring indefinitely
journeyGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener()
{
#Override
public void onCameraChange(final CameraPosition position)
{
Basically, what I need is a function where I can provide the center LatLng and the markerLatLng and it will automatically calculate the LatLngBounds making sure my center is within the center of the LatLng bounds, and then I can simply use
public static CameraUpdate newLatLngBounds (LatLngBounds bounds, int width, int height, int padding)
as shown in the link below
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory#newLatLngBounds(com.google.android.gms.maps.model.LatLngBounds,int, int,int)
If you need anymore clarification please do tell me
You need to calculate LatLngBounds from two points:
Your marker position and
Place on the opposite side of your current center.
The second is calculated like this:
LatLng other = new LatLng(2 * center.latitude - position.latitude, 2 * center.longitude - position.longitude);
See LatLngBoundsUtils.fromCenterAndPositions for a general solution.
Use googleMap.getProjection().getVisibleRegion() to get all four corners of screen as LatLng values forming trapezium. Calculate where trapezium intersects with line drawn through center and your marker. Scale factor is (distance from center to marker / distance from center to intersection point). Now just scale trapezium with this scale factor relative to center. This is new visible regison.
You may also use getVisibleRegion().latLngBounds to simplify calculation, but note that some areas of returned rectangle are actually not visible.

android change bounds of google map

I am working on a project in android which requires me to change the bounds of a google map(from bottom only). Currently I am trying by adding padding at the bottom, but this process is slow and the screen also flickers.
Is there any better way to do this?
I would like the user to be able to drag the bottom of the map to change the bounds.
I see a lot of AJAX/JS resouces, but this is a native component.
Best
I think it will be a huge amount of work to let the user actually drag the bottom of your map view. what is much easier and faster to do is to create to 2 map fragments of your map and on click of the map change the fragment back and foreword between the two.
this way the user can change the size of the map the he works with.
You can zoom map to added points.
Here is some pseudo code:
LatLngBounds.Builder bc = new LatLngBounds.Builder();
for (LatLng item : points) {
bc.include(item);
}
LatLngBounds bounds = bc.build();
#SuppressWarnings("deprecation")
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight(), 50);
mMap.moveCamera(cu);
Bounds will be changed to added points. Help It hope

Categories

Resources