Get Google Maps to automatically zoom in on my Current Location?
android, google-maps
Solution
Try this is simple solution for your question
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
and also..
It's possible to change location, zoom, bearing and tilt in one go. It is also possible to set the duration on the animatecamera call.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Have a look at the docs here:
https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera
Problem
Possible duplicate of Google Maps v2 - set both my location and zoom in ``` protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_g_maps); GoogleMap googleMap; LatLng myPosition; SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); googleMap = fm.getMap(); googleMap.setMyLocationEnabled(true); LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); if(location!=null){ double latitude = location.getLatitude(); double longitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longitude); myPosition = new LatLng(latitude, longitude); } } } ``` I've tried adding: ``` CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)); CameraUpdate zoom=CameraUpdateFactory.zoomTo(15); googleMap.moveCamera(center); googleMap.animateCamera(zoom); ``` or ``` googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) ); ``` but Google Maps doesn't automatically zoom in once I've hit the button which calls this method. I still get the Google UI which I can click to zoom in on my Location, but I want it to automatically zoom in. Any help please?