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.
Related
i am initiating direction api to draw polyline,calling asyncTask in oncreate, but its not getting called second time onwords even opening activity again and again. after removing app from recent apps and opening it again works for a single time.here is my code.
private void moveCamera(LatLng originLatLng, LatLng endLatLng) {
mMap.addMarker(new MarkerOptions().position(currentLatLng).title("Starting Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.pickup_location)));
HelperFunctions.addOverlay(currentLatLng, mMap, SpeedMediatorActivity.this);
if (endLatLng != null) {
mMap.addMarker(new MarkerOptions().position(destLatlng).title("Destination Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.pickup_location)));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(originLatLng);
builder.include(endLatLng);
LatLngBounds bounds = builder.build();
int padding = 50; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
String url = getDirectionsUrl(currentLatLng, destLatlng);
// Start downloading json data from Google Directions API
new DownloadTask().execute(url);
} else {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(originLatLng, 16));
}
// new FetchURL(SpeedMediatorActivity.this).execute(getUrl(originLatLng, endLatLng, "driving"), "driving");
}
Can someone help me please.
Hi guys my problem is about mapView. I want to set the camera to a new position but when i call SetNewPosition the log tell to me that coordinates are changed but the mapview on the screen shows always the same place.
public void SetNewPosition(double log,double lat)
{
Log.i("Current position",""+mappa.getCameraPosition());
LatLng latLng= new LatLng(log, lat);
CameraUpdate cameraUpdate= CameraUpdateFactory.newLatLng(latLng);
mappa.moveCamera(cameraUpdate);
mappa.clear();
map.invalidate();
map.postInvalidate();
Log.i("Changed position",""+mappa.getCameraPosition());
}
Can anyone tell where is my error?
This is what you may want to use :
LatLng latLng = new LatLng (log, lat);
LatLngBounds.Builder builder = new LatLngBounds.Builder ();
builder.include (latLng);
LatLngBounds bounds = builder.build ();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds (bounds, 0);
googleMap.animateCamera (cameraUpdate);
Can someone explain how I can use the Google maps API in my project?
Cause I want to draw a marker on the map while moving the map with my finger.
But I don't know how to achieve this.
I am using the following code:
camCenter = mMap.getCameraPosition().target;
if (camCenter != null && camCenter != camCenterFirst) {
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(camCenter.latitude,camCenter.longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
camCenterFirst = camCenter;
}
First you have to add a marker to your Map like this:
LatLng pos = new LatLng(53.557115, 10.023159);
Marker m = mMap.addMarker(new MarkerOptions().position(pos).title(""));
The example above, gives you a default position of the marker.
Then you can change the position of the marker with following command:
m.setPosition(new LatLng(latitude,longitude));
Hope this helps!
public void showOnMap(Double lat,Double lng,String place)
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat,
lng)).title(""+place);
mMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(lat, lng)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
double radiusInMeters = 5000.0;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(new LatLng(lat,lng)).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
mMap.addCircle(circleOptions);
}
I have done a complete demo of what you are looking for,
Check this Github code
https://github.com/cseshaiban/GeoApp
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.
From a Thread I start some code on UI in order to zoom a map and to position it.
While the positioning works fine (New York), the zoom is ignored (always half of the globe). I have tried with all possible zoomOld values, but it's all the time the same. What is wrong in my code?
runOnUiThread(new Runnable() {
public void run() {
zoomOld = 1;
CameraUpdate forget = CameraUpdateFactory.zoomTo(zoomOld);
map.animateCamera(forget);
double targetLat = 40.7;
double targetLon = -74.1;
LatLng p = new LatLng(targetLat,// 3.0.1
targetLon);
map.moveCamera(CameraUpdateFactory.newLatLng(p));
Try this in my case works fine.Hope this to help you.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mCongressLocation).zoom(16).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));