I need to use few features of both CameraPosition and CameraBounds, Like my specific requirement is to move the camera on a bearing and tilt it on a few degrees as well as there are other markers on the map which i want to cover while i perform a camera update .
CameraUpdateFactory class have two different methods on two different purpose.
1) newCameraPosition 2) newLatLngBounds
But i doubt if i can use both of them simultaneously to get the result i want.
CameraPosition works as below :
LatLng current_lat_lng=new LatLng(lat, lon);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(current_lat_lng) // Sets the center of the map to Mountain View
.zoom(googleMap.getCameraPosition().zoom) // Sets the zoom
.bearing((float)mapHeading) // Sets the orientation of the camera to a bearing
.tilt(0) // Sets the tilt of the camera to 0 degrees
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
CameraBounds work as below :
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
if (marker_Searched != null) {
builder.include(marker_Searched.getPosition());
}
if (marker_selected_taxi != null) {
builder.include(marker_selected_taxi.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 300; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.moveCamera(cu);
Do anyone have tried any workaround with this ?
Animate your camera to the LatLngBounds with CancellableCallback, then in onFinish get zoom value (map.getCameraPosition().zoom) then apply this value into CameraPosition. It works for me fine.
Related
I'm having a bit of a struggle using CameraPosition because I can't have a proper zoom.
For me, a proper zoom would be the distance between a Marker and my current position.
However, I managed to get a proper zoom using CameraUpdateFactory, but the I lost all the other attributes (orientation (looking always north) and bird-eye (45 degrees view)).
I'm balanced between this (doesn't have the right zoom):
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(current_location)
.zoom(mGoogleMap.getCameraPosition().zoom)
.bearing(location.getBearing())
.tilt(birdEyesAngle)
.build();
and this (right zoom, but missing other attributes):
CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);
Is there any way to have both correct zoom and correct orientation/bird-eye ?
Hope you'll help,
Thanks
Try this:
Get the CameraUpdate object first using the newLatLngBounds method:
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);
The following code I put in my onMapReady() method:
LatLng pos = new LatLng(51.516667, 12.388889);
LatLng pos1 = new LatLng(53.516667, 14.388889);
MarkerOptions markerOptions = setUserMarker(pos);
if(markerOptions != null) {
markerOptions.title(campusLocationName);
mMap.addMarker(markerOptions);
}
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(pos);
b.include(pos1);
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(b.build(), 20);
mMap.animateCamera(cu, 10, new GoogleMap.CancelableCallback() {
#Override
public void onFinish() {
Log.e(TAG, "Start animate onFinish");
CameraPosition cp = new CameraPosition.Builder()
.zoom(mMap.getCameraPosition().zoom)
.target(pos)
.tilt(45.0f)
.bearing(35.0f)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
// mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
}
#Override
public void onCancel() {
Log.e(TAG, "Start animate onCancel");
}
});
The result from my device:
Using the CancelableCallBack you can make the changes to the camera position as before, but if you do not change the zoom factor, the camera keeps the old zoom factor, you just set the bearing and tilt as you like.
I used this code to get particular location in center.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(33.7167, 73.0667), 4))
It worked somehow but after putting 30 no of markers simultaneously. I didn't get location in center.
this is my snippet of code
http://i.stack.imgur.com/jvATG.png
http://i.stack.imgur.com/RPsNx.png
http://i.stack.imgur.com/ImmD8.png
http://i.stack.imgur.com/a930b.png
You can update camera to the center of bounds of markers. For that -
Create LatLngBounds.Builder object and add all the markers in it.
LatLngBounds.Builder builder = new LatLngBounds.Builder();
While adding marker store it to variable like
Marker marker1 = mMap.addMarker(....);
Marker marker2 = mMap.addMarker(....);
Then add all markers in it using
builder.include(marker1.getPosition());
builder.include(marker2.getPosition());
Then build LatLngBounds
LatLngBounds bounds = builder.build();
Then create CameraUpdate and animate camera to relative position
int padding = 0; // offset from edges of the map in pixels
CameraUpdate camUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(camUpdate);
And done.
Refer this answer for more details.
Hope it'll work.
If you look at google navigation, it always keeps the driver marker close to bottom and when you move the camera it offers to reset it back to bottom. I'm wondering how to achieve the same thing given a marker.
There was a similar question before, but the offered answers do not consider the map to be tilted which results in wrong projection.
The current location marker always to bottom of the screen while moving also. For that we have to set
map.setPadding(0,320,0,0);
So, we set pdding top to the map 320 then it takes some space from the top of the screen.
In your final code like this
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(newLatLng)
.zoom(18)
.bearing(0)
.tilt(0)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.setPadding(0,320,0,0);
Nima, there are different way to achieve this behaviour by tweaking values in camera positions.
For instance you have 2 geo location latlng information available with you, UserLocation and DestinationLocation find the midpoint of this location and set the camera target at it. And then you can move the camera to zoom level which cover both geolocation with proper padding of top and bottom by specifying the bearing value.
//First build the bounds using the builder
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds;
builder.include(userLocation);
builder.include(destinationLocation);
bounds = builder.build();
// define value for padding
int padding =20;
//This cameraupdate will zoom the map to a level where both location visible on map and also set the padding on four side.
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding);
mMap.moveCamera(cu);
// now lets make the map rotate based on user location
// for that we will add the bearing to the camera position.
//convert latlng to Location object
Location startLocation = new Location("startingPoint");
startLocation.setLatitude(userLocation.latitude);
startLocation.setLongitude(userLocation.longitude);
Location endLocation = new Location("endingPoint");
endLocation.setLatitude(destinationLocation.latitude);
endLocation.setLongitude(destinationLocation.longitude);
//get the bearing which will help in rotating the map.
float targetBearing = startLocation.bearingTo(endLocation);
//Now set this values in cameraposition
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(bounds.getCenter())
.zoom(mMap.getCameraPosition().zoom)
.bearing(targetBearing)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Currently I work with Google-Map-v2 and I want to show Direction between two Markers. Everything is ok and direction between two Markers calculate and draw fine.But only one problem is remain.
The problem is Zoom level between these two markers in Google-Map is too much. I Search in SO and find a solution for changing Zoom Level by Following code :
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(dest);
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 50);
mMap.animateCamera(cu);
Now it's better But still zoom level is not good. Is it possible to do some fixes on Code ? ( if I can some padding to view is fixed )
I Also change 50 to 6,12,... But nothing changed.
You are using the correct code, and the value (50) needs to be increased if you want to zoom less (be outer) or decreased to 0 if you want to be in the smallest area containing the two markers (you can skip the value in case).
If you set a value of 150 or more, and the level is too much you can use the animation callback to zoom out after the LatLng zoom:
Google API
com.google.android.gms.maps.GoogleMap.CancelableCallback)
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(dest);
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 50);
mMap.animateCamera(cu, new GoogleMap.CancelableCallback(){
void onCancel(){}
void onFinish(){
CameraUpdate zout = CameraUpdateFactory.zoomBy(-3.0);
mMap.animateCamera(zout);
}
});
This should zoom to the LatLng and when finished, zoom back of 3 levels.
try this:
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(yourlatlng)
// .bearing(targetBearing)// you can ignore it
.zoom(12)// your zoom value
// .tilt(40) // angle of view
.build();
mMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
500,
null);
actually I'm working on an android application using the Google Map APIv2, so far I'm trying to display some markers on a the map, also i want to have all the markers inside my camera view, for this I'm using
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds;
int padding = 0;
//markMap is a hashmap populated with my markers positions.
HashMap<String, LatLng> markMap = new HashMap<String, LatLng>();
for (Entry<String, LatLng> entry : markMap.entrySet())
{
map.addMarker(new MarkerOptions().position(entry.getValue()));
builder.include(entry.getValue());
}
bounds = builder.build();
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.animateCamera(update);
This works perfectly fine, but there is only one problem with it, and that is : my current camera zoom level only includes the marker points, but the marker icon itself is usually, totally or partially, out of bounds.
Any suggestions on what i should do so i can have a the markers icons included inside my camera view ?
Why not just add some padding:
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, 50);
I don't see why CameraUpdate should take into consideration the width of the marker itself. It includes the coordinates not the marker itself.
Another option could be:
CameraPosition cameraPosition =
new CameraPosition.Builder().target(ll).zoom(16).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
You can see more about "Camera Position" here