Android Google Maps API V2 Zoom to Current Location

I’m trying to mess around with the Maps API V2 to get more familiar with it, and I’m trying to start the map centered at the user’s current location. Using the map.setMyLocationEnabled(true); statement, I am able to show my current location on the map. This also adds the button to the UI that centers the map on my current location.

I want to simulate that button press in my code. I am familiar with the LocationManager and LocationListener classes and realize that using those is a viable alternative, but the functionality to center and zoom in on the user’s location seems to already be built in through the button.

  • ActionBar Action Items not showing
  • SSLSocket hangs at getInputStream when android device is in wifi
  • proguard gradle debug build but not the tests
  • How to get a list of the activity history stack?
  • Authenticate user logged in with FB at my server using the Android Account Manager
  • android service startService() and bindService()
  • If the API has a method to show the user’s current location, there surely must be an easier way to center on the location than to use the LocationManager/LocationListener classes, right?

    Related posts:

    Custom Android views with Kotlin
    Android Studio, change the editor background color (not the whole theme)
    Observed package id 'build-tools;20.0.0' in inconsistent location
    Intent putExtra ArrayList<NameValuePair>
    Failed to resolve:
    App builders: how to evaluate their worth?
  • not finding android sdk (Unity)
  • ActionBarSherlock tab height not adjustable on Jelly Bean
  • Crop square image to circle - Programmatically
  • Why use Android Picasso library to download images?
  • How to add toolbars to AppCompatPreferenceActivity?
  • Android gradle signingConfig error
  • 8 Solutions collect form web for “Android Google Maps API V2 Zoom to Current Location”

    Try this coding:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (location != null)
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
            .zoom(17)                   // Sets the zoom
            .bearing(90)                // Sets the orientation of the camera to east
            .tilt(40)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder

    After you instansiated the map object (from the fragment) add this –

    private void centerMapOnMyLocation() {
        location = map.getMyLocation();
        if (location != null) {
            myLocation = new LatLng(location.getLatitude(),

    if you need any guidance just ask but it should be self explantory – just instansiate the myLocation object for a default one…

        mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            public void onMyLocationChange(Location location) {
                    CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
                    CameraUpdate zoom=CameraUpdateFactory.zoomTo(11);
    youmap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentlocation, 16));

    16 is the zoom level

    try this code :

    private GoogleMap mMap;
    LocationManager locationManager;
    private static final String TAG = "";
    protected void onCreate(Bundle savedInstanceState) {
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        arrayPoints = new ArrayList<LatLng>();
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng myPosition;
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
        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);
            LatLng coordinate = new LatLng(latitude, longitude);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 19);


    Dont forget to add permissions on AndroidManifest.xml.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    private void setUpMapIfNeeded(){
        if (mMap == null){
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(;//invoke of map fragment by id from main xml file
         if (mMap != null) {
             mMap.setMyLocationEnabled(true);//Makes the users current location visible by displaying a blue dot.
             LocationManager lm=(LocationManager)getSystemService(LOCATION_SERVICE);//use of location services by firstly defining location manager.
             String provider=lm.getBestProvider(new Criteria(), true);
             Location loc=lm.getLastKnownLocation(provider);
             if (loc!=null){
        // Initialize map options. For example:
        // mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    public void onLocationChanged(Location location) {
       LatLng latlng=new LatLng(location.getLatitude(),location.getLongitude());// This methods gets the users current longitude and latitude.
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));//Moves the camera to users current longitude and latitude
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,(float) 14.6));//Animates camera and zooms to preferred state on the user's current location.
        // TODO Auto-generated method stub

    This is working Current Location with zoom for Google Map V2

     double lat= location.getLatitude();
     double lng = location.getLongitude();
     LatLng ll = new LatLng(lat, lng);
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));

    Major point here is to get the geocoordinates of the current location. You can use GPS library to get that. Once you have the geocoordinates, its trivial. Refer the following for more details:
    How to get the current location latitude and longitude in android

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.