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!
Related
Currently, I am working with google map and may have confusion between this two methods. I don't differentiate working of this two method so, can anyone explain me what is difference between zoomBy() and zoomTo()
Code:
mMap.animateCamera(CameraUpdateFactory.zoomBy(zoomLevel));
mMap.animateCamera(CameraUpdateFactory.zoomTo(zoomLevel));
CameraUpdateFactory.zoomTo(float) gives you a CameraUpdate that
changes the zoom level to the given value, while keeping all other
properties the same.
CameraUpdateFactory.zoomBy(float) and
CameraUpdateFactory.zoomBy(float, Point) give you a CameraUpdate that
increases (or decreases, if the value is negative) the zoom level by
the given value. The latter fixes the given point on the screen such
that it remains at the same location (latitude/longitude) and so it
may change the location of the camera in order to achieve this.
From official documentation here
So in short zoomTo just changes the zoom level to the given value, while the zoomBy increases or decreases ( e.g. with zoomTo(20) your camera will have the zoom level set to 20, but if your zoom level was 20 and you call zoomBy(-5), your zoom level will become 15)
public static CameraUpdate zoomBy (float amount, Point focus)
Returns a CameraUpdate that shifts the zoom level of the current
camera viewpoint.
A point specified by focus will remain fixed (i.e., it corresponds to
the same lat/long both before and after the zoom process).
This method is different to zoomTo(float) in that zoom is relative to
the current camera.
For example, if the LatLng (11.11, 22.22) is currently at the screen
location (23, 45). After calling this method with a zoom amount and
this LatLng, the screen location of this LatLng will still be (23,
45).
public static CameraUpdate zoomTo (float zoom)
Returns a CameraUpdate that moves the camera viewpoint to a particular
zoom level.
In live track app I want to animate camera above marker and want to put marker always at bottom in newLocationChanged.
If I am building CameraPosition without zoom, It works fine.
But with zoom also I need to achieve same.
Use the screen boundaries to get the desired position:
As per documentation:
getBounds()
Return Value: LatLngBounds
Returns the lat/lng bounds of the current viewport. If more than one copy of the world is visible, the bounds range in longitude from -180 to 180 degrees inclusive. If the map is not yet initialized (i.e. the mapType is still null), or center and zoom have not been set then the result is null or undefined.
(I would want to put screenshots put my reputation is low to put them...)
Good Day.I Have two directions(markers) on google maps api(android) between which i want camera to be center,and camera being centered there.But problem is that one marker comes to one edge of screen and another marker to another edge of screen!I don't want it to be like that,i want just to zoom specified level so markers will be center in google maps view and won't be place on edges of screen.I tried to put even 0 zoom level in camera update factory of google maps,but nothing happens at all.Here is my code how do i achieve that
} finally {
com.google.android.gms.maps.model.LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(new LatLng(Double.parseDouble(tolat), Double.parseDouble(tolong)));
boundsBuilder.include(new LatLng(Double.parseDouble(fromlat), Double.parseDouble(fromlong)));
final LatLngBounds bounds = boundsBuilder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));
}
you say you tried to put 0 as the zoom level. You mean this call CameraUpdateFactory.newLatLngBounds(bounds, 0) right?
The second parameter is the padding not the zoom level. Set it up (example to 10) and you have a padding to the outer edges.
https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory
I hope it helps.
I'm developing an app that is using a google map to show your location. I want to rotate the map around the users location. I use getOrientation(R, orientation) (and adjust for true north) to get the device rotation which is working fine but i want the user location to be near the bottom of the screen. I have set the padding on the map to a quarter of the screen height
mMap.setPadding(0,screenHeight/4,0,0);
What hI have noticed is that the rotation is still done around the point at the centre of the screen NOT the padded centre.
This is the code I'm using in onSensorChanged
currentZoom = mMap.getCameraPosition().zoom;
CameraPosition currentPlace = new CameraPosition.Builder()
.target(new LatLng(currentNode.getLatitude(), currentNode.getLongitude()))
.bearing(degrees).tilt(60).zoom(currentZoom).build();
mMap.stopAnimation();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(currentPlace),40,null);
Is there a way to change the anchor point of camera rotation?
I was trying to solve this problem for several hours. The only solution I found is not very good, but it works:
For example, if you want to move the user 200dp below the center:
1) Set map fragment height to its height + 200dp (Then the map will go 200dp below the screen)
2) This works fine, but it hides Google logo. To preserve the Google logo, set map padding top and bottom to 200dp.
mGoogleMap.setPadding(0, dp2px(200),0 , dp2px(200));
That solves the problem
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.