Map positioned, but not zooming - android

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));

Related

CameraPosition with bounds using GoogleMap

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.

MapView.moveCamera() show always the same camera

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);

Set CameraPosition to my location, but show other markers also

I am designing an Android app that should show multiple markers on map. I can do that easily. My problem is that I need to show all the markers around my position, and the CameraPosition needs to be in focused in my position. I can easily do each on them separately, but what I need is showing them together, my location in the middle of the map, and the markers around my location.
Any idea how can I achieve that? Thanks in advance!
Set your zoom to a higher level so it can zoom out and see more of your map around your current location.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(yourLatLng).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
The Functionality you need is to keep all the selected markers on the maps and it it should be visible at a single time. you have to keep all those points in the visible region with myLlocation (your current location) at the center of the screen always.
First keep your myLocation inside an latlng variable and all the other latlng which are to be inside the bounds inside another ArrayList of LatLng. Also define some variables to keep track of operation.
LatLng myLocation=null;
ArrayList<LatLng> latlngs = null; // the arraylist of other latlng;
boolean are_all_inside_viewport = false;
float current_zoom = googleMap.getCameraPosition().zoom;
//initialise these variables myLocation and latlngs;
Then animate your camera to myLocation which will be placed into the center of the map. put a low zoom level here.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(14)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now check for each latlng in the ArrayList of they are in the google maps view port. if they are inside then its ok. if not then decrease a zoom level of the maps and animate the camera again.
while (!are_all_inside_viewport) {
are_all_inside_viewport = true;
for (LatLng latLng : latlngs) {
if (!bounds.contains(latLng)) {
are_all_inside_viewport = false;
break;
}
}
bounds = googleMap_local.getProjection().getVisibleRegion().latLngBounds;
cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(current_zoom - 1)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
This will go on checking for each latlng inside the ArrayList until all the latlng are inside the viewport.
Hope this answer helps you.

Android: How can I use the Google maps

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

How to apply Camera bounds with CameraPosition in google map

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.

Categories

Resources