Android: Google maps v2, zoom is not working - android

Programmatically changing camera zoom is not working for me. Take look at the code:
public void moveToMyLocation(GoogleMap map)
{
Criteria crit = new Criteria();
String provider = locationManager.getBestProvider(crit, true);
Location loc = locationManager.getLastKnownLocation(provider);
LatLng latlng=new LatLng(loc.getLatitude(),loc.getLongitude());
float czoom = map.getCameraPosition().zoom; // eq 2.0
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,(float) 19.6));
czoom = map.getCameraPosition().zoom; // STILL eq 2.0!!
}
I can still zoom map using default controls.
m.

This is what worked for me:
map.animateCamera(CameraUpdateFactory.newLatLng(
LatLng(it.position.latitude, it.position.longitude)))
Handler().postDelayed({
map.animateCamera(CameraUpdateFactory.zoomIn())
}, 500)

you can use this to zoom directly without the animation :
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );

Related

How to move android google map camera with constant velocity and zoom

I cannot find the way to move android google map camera with constant velocity and zoom.
For example, if the current zoom is 15, and calling
CameraPosition pos= CameraPosition
.builder(map.mMap.getCameraPosition())
.target(target)
.zoom(15)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos), duration * 1000, null);
it modifies the zoom 15 - > 12 -> 15
(the mid value depends on the distance)
My target is to show an object moving on the map with constant velocity and always keep it in the center of the map
Tried to call moveCamera in the ValueAnimator of the object, but then the map jumps. I need to move it smoothly
Use fused API for location update and move the camera to the updated location
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
if(location!=null){
try{
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
String addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1).get(0).getAddressLine(0);
marker_origin = new MarkerOptions().
position(latLng).title(addresses)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mMap.addMarker(marker_origin);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
getPlacesData(latLng, "restaurant");
}catch (Exception e){
e.printStackTrace();
}
}
}
});
For further details check the android developer documentation
https://developer.android.com/training/location/receive-location-updates.html

Location query in Google maps in android

I have made an app utilizing Google Maps. I have added a marker of my current location in it. However, as soon as I open the app, I want it to zoom in to my location instead of showing the whole world at the 95302 23535919711008489 location first. How do I accomplish this?
Use animateCamera() as below
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
Please read the docs
LatLng cameraLatLng = new LatLng(savedLat, savedLng);
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.animateCamera(CameraUpdateFactory
.newLatLngZoom(cameraLatLng, 7))
savedLat, savedLng your current location longitude and latitude.You can change zoom number ( 7 ) to your fitting.

How to stop zooming in google map api 2 after updating current location every second?

I have developed an android app which get users current location at every second. but while updating location every second it zoom in by 2x at every update at every time so please help me to set constant zoom level which will not zoom after updating every second, I used this listener in onCreate
map.setOnMyLocationChangeListener(myLocationChangeListener);
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange (Location location) {
LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());
Bitmap bitmapicon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("My Location").icon(BitmapDescriptorFactory.fromBitmap(bitmapicon)));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 5.0f));
}
};
You have to change the
map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 5.0f));
to
map.animateCamera(CameraUpdateFactory.newLatLng(loc);
try this
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(),loc.getLongitude()), 5.0f));
see this for further reference reference
You need to declare a CameraPosition() object like this:
mCameraPosition = new CameraPosition(mLatLong, 18, 30, 112.5F);
Have a look at the constructor here
CameraPosition(LatLng target, float zoom, float tilt, float bearing)
And finally pass this Camera position to your map like :
mGoogleMapOptions.camera(mCameraPosition);
Then pass the same zoom level when the location changes:
CameraUpdateFactory.newLatLngZoom(LatLng, float)
So in this case your zoom level will be 18 along with the new location.
As stated on this link

Animating Camera with bounds returns nullPointerException

I am develepoing an app that needs to view various map points at the same time. I am trying to animate camera with bounds and Builder, but it's returning null every time, and I checked the coordinates and the .build() and all of them have data. this is the code.
LatLngBounds.Builder builderMap = new LatLngBounds.Builder();
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
if(location!=null)
{
Log.i("MapACtivity","builder includes myLocation");
builderMap.include(new LatLng(location.getLatitude(),location.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
for(CircleContact myContact : newCircle.getMembers())
{
if(myContact.getLatitude()!=0 && myContact.getLongitude()!=0)
{
Log.i("MapACtivity","builder includes locations " + myContact.getDevice());
Log.i("MapACtivity","Lat " + myContact.getLatitude()+ " longitude" +myContact.getLongitude());
builderMap.include(new LatLng(myContact.getLatitude(),myContact.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
}
if(coordinate_bounds>1)
{
Log.i("CircleFragment","builderMap.build()" + builderMap.build().getCenter());
Log.i("CircleFragment","builderMap.build()" + builderMap.build().northeast);
Log.i("CircleFragment","builderMap.build()" + builderMap.build().southwest);
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
}
The log that I get is :
builderMap.build()lat/lng: (-11.075408249999999,-75.85188305)
builderMap.build()lat/lng: (-10.078819,-74.748112)
builderMap.build()lat/lng: (-12.0719975,-76.9556541)
But it's returning null in this line:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
and it is not the map because, before I update the camera to my location, and that doesn't crash, this is the code before trying to get all positions inside the map and it is working fine:
MapView mMapView;
mMapView = (MapView)getActivity().findViewById(R.id.map_locations);
mMap = mMapView.getMap();
mMap.addMarker(myMarker);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(Location, 15);
mMap.animateCamera(yourLocation);
This comes from the official google maps android page:
Note: Only use the simpler method newLatLngBounds(boundary, padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary, width, height, padding) at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.
And this is how they are setting bounds:
private GoogleMap mMap;
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));

google maps api circular radius and marker together

I was trying few tricks using google maps api. I am trying to
Get the lat log of my current location and zoom in on the map.
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
Create a radius around my current location on the google map without the 'dot'. Just a circular radius around my location
Then use Marker to manually tap on a location on the map very close to me and get the lat long.
Basically what I am trying is I need to accurately place a marker on a building near me.
I hope I made sense. Any advise will be helpful.
Thanks
Shashi
You can try this
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng myLoc = null;
myLoc = new LatLng(location.getLatitude(),
location.getLongitude());
CameraUpdate center = CameraUpdateFactory.newLatLng(myLoc); //optional
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12); //optional
googleMap.addMarker(new MarkerOptions()
.position(myLoc)
.title("title you want to show")
.snippet("anything you want to show")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

Categories

Resources