CameraUpdateFactory zoom to does not zoom map - android

I have a method that zooms the map to the user's specified zoom level but it will not zoom using CameraUpdateFactory.zoomTo(9f);
However It will zoom correctly if I do this
CameraPosition position = new CameraPosition.Builder().target(location).zoom(9).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(position));
but this requires that I have a location to zoom to.
According to the docs it looks like I just access the zoomTo method statically but when I do that nothing happens.
Am I using the first method incorrectly?

The method CameraUpdateFactory.zoomTo does not directly alter the map, instead it returns a CameraUpdate value . This value will have the new zoom level set by you and the other parameters like target, bearing, etc will be retained as they are. Now for the value to take effect, you need to apply the CameraUpdate to the map using animateCamera(CameraUpdate) or moveCamera(CameraUpdate)

Related

What is difference between ZoomTo and ZoomBy in google map

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.

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!

Force animateCamera to complete animation

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.

How to put marker at bottom of map instead of center(by default) of map with zoom enabled in android?

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.

Android Map Zoom Level for Current Location and Destination

I am displaying the user's current position and their intended destination using Google maps, MappingOverlayActivity and ItemizedOverlay.
What is the best way to firstly set the appropriate zoom level so that both locations are displayed on the map on the screen with the maximum area covered by the map, i.e rather than the current location being in the centre, having the centre being in the middle of the 2 points.. Also when the user's current location changes, how can I then recalulate and reset the appropriate zoom level to incorporate the new current location.
You use methods from MapController (which you obtain using mapView.getController()).
Specifically animateTo (or moveTo), and zoomToSpan.
Assuming you have two GeoPoints
GeoPoint current;
GeoPoint destination;
do something like this
mapController.zoomToSpan(Math.abs(current.getLatitudeE6() - destination.getLatitudeE6()), Math.abs(current.getLongitudeE6() - destination.getLongitudeE6());
mapController.animateTo(new GeoPoint((current.getLatitudeE6() + destination.getLatitudeE6())/2, (current.getLongitudeE6() + destination.getLongitudeE6())/2));
so you calculate the span (difference) and center (average) of the points.
For the center of the coordinates you might need to do some math
http://en.wikipedia.org/wiki/Centroid

Categories

Resources