Android Google Maps API V2 Zoom to Current Location

How do I zoom in automatically to the current location in Google Maps API for Android?

Change from:

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

to:

float zoomLevel = 16.0f; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));

public static CameraUpdate newLatLngZoom (LatLng latLng, float zoom)

Returns a CameraUpdate that moves the center of the screen to a
latitude and longitude specified by a LatLng object, and moves to the
given zoom level.

Parameters latLng a LatLng object containing the desired latitude and
longitude. zoom the desired zoom level, in the range of 2.0 to 21.0.
Values below this range are set to 2.0, and values above it are set to
21.0. Increase the value to zoom in. Not all areas have tiles at the largest zoom levels. Returns a CameraUpdate containing the
transformation.

Android Google Maps v2 - set zoom level for myLocation

It's doubtful you can change it on click with the default myLocation Marker. However, if you would like the app to automatically zoom in on your location once it is found, I would check out my answer to this question

Note that the answer I provided does not zoom in, but if you modify the onLocationChanged method to be like the one below, you can choose whatever zoom level you like:

@Override
public void onLocationChanged(Location location) {
if( mListener != null ) {
mListener.onLocationChanged( location );

//Move the camera to the user's location and zoom in!
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));
}
}

Change zoom level Google Maps Api v2 on app start

You can set zoom level like following:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));

Where point is your LatLng position. In this way you can center the map in that point with given zoom level.

Complete method:

@Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
//newLatLngZoom(LatLng , ZoomLevel) -> choose your zoom level
// and change my 'point' with yours
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}

EDIT:

If you want to get the dot coords, you can try this:

Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getlatitude() , loc.getLongitude());

and use that point as center.

Complete method:

@Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
Location loc = map.getMyLocation();
if(loc != null){
LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}
}

This could cause a NullPointerException beacuse loc could be null.

OTHER SOLUTION

If you want to get only first time the coordinates, you should work in onLocationChanged, using a boolean variable to set first call.

Declare it CRDS_CALL = false;

  @Override
public void onLocationChanged(Location location) {
if (mMapLocationListener != null) {
mMapLocationListener.onLocationChanged(location);
if(!CRDS_CALL){
LatLng point = new LatLng(location.getLatitude(), location.getLognitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
CRDS_CALL = true;
}
}
}

In this answer i use map, but you have to use your mapFragment, but if you want to use it in other methods over onCreate, you have to declare outside of it.

Add this just before the onCreate

SupportMapFragment mapFragment;

And inside it, use it like follwing:

mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

In that way, you can use mapFragment in other methods

Google maps api v2 zooming near the marker

pass your current location in this function where you have placed your marker.

private void moveToCurrentLocation(LatLng currentLocation)
{
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

}

Google Maps v2 - set both my location and zoom in

You cannot animate two things (like zoom in and go to my location) in one google map?

From a coding standpoint, you would do them sequentially:

    CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

map.moveCamera(center);
map.animateCamera(zoom);

Here, I move the camera first, then animate the camera, though both could be animateCamera() calls. Whether GoogleMap consolidates these into a single event, I can't say, as it goes by too fast. :-)

Here is the sample project from which I pulled the above code.


Sorry, this answer is flawed. See Rob's answer for a way to truly do this in one shot, by creating a CameraPosition and then creating a CameraUpdate from that CameraPosition.



Related Topics



Leave a reply



Submit