Search code examples
androidzoomingsupportmapfragment

SupportMapFragment set google maps Zoom level


how can i set the zoom level of a SupportMapFragment ? I tried some answers I found in similar questions, like this one answered buy a guy named Rob who proposed:

LatLng currentLocation = mService.getCurrentLocation();
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(currentLocation)    // Sets the center of the map to the current location of the user
            .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
    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

But this solution didn't work. So, how can I set the Zoom level knowing that I use a SupportMapFragment.


Solution

  • if your current location is not null then try this code it will work

    LatLng currentLocation = mService.getCurrentLocation();
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentLocation , 16);
    mMap.addMarker(new MarkerOptions().position(location).title(""));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
    mMap.animateCamera(cameraUpdate)