Android Google Maps v2 - set zoom level for myLocation
Asked Answered
O

13

126

Is it possible to change the zoom level for myLocation with the new Google Maps API v2?

If you set GoogleMap.setEnableMyLocation(true);, you get a button on the map to find your location.

If you click on it, then the map will bring you to your location and zoom it in to some level. Can I change this zoom to be less or more?

Occasion answered 18/12, 2012 at 11:55 Comment(1)
do you have any idea of mylocation button zoom level? I want that value.Diwan
M
250

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));
    }
}
Mellifluent answered 18/12, 2012 at 15:43 Comment(1)
Be careful using animateCamera() in your onMapReady(), this may lead map lag with lite yellow background. For map init it is better to use moveCamera() (same signature)Hebbe
C
164

You can also use:

mMap.animateCamera( CameraUpdateFactory.zoomTo( 17.0f ) );    

To just change the zoom value to any desired value between minimum value=2.0 and maximum value=21.0.

The API warns that not all locations have tiles at values at or near maximum zoom.

See this for more information about zoom methods available in the CameraUpdateFactory.

Caiaphas answered 19/3, 2013 at 16:45 Comment(3)
What is the default zoom level??Pillow
14 is the default zoom levelTandi
Why did you choose 17.0f?Pillow
G
33

with location - in new GoogleMaps SDK:

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(chLocation,14));
Goering answered 13/10, 2015 at 10:14 Comment(1)
Is there a difference between newLatLngZoom() and zoom() which a lot of other answers have referenced? In app, from end user perspective, these look the same.Superfluous
P
24

Here are the approximate zoom levels and what they do :

1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings

so you could do something like this to zoom to street level for example (note the "15f" below is street level) :

 override fun onMapReady(googleMap: GoogleMap?) {
    googleMap?.mapType = GoogleMap.MAP_TYPE_NORMAL
    googleMap?.addMarker(MarkerOptions()
            .position(LatLng(37.4233438, -122.0728817))
            .title("cool place")

            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)))

    googleMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.4233438, -122.0728817), 15f))

note: just so you know different locations can have different max zoom levels. try to use googleMap.maxZoomLevel if you want to get the max or min zoom levels.

Professorate answered 15/6, 2018 at 8:27 Comment(0)
V
15

Slightly different solution than HeatfanJohn's, where I change the zoom relatively to the current zoom level:

// Zoom out just a little
map.animateCamera(CameraUpdateFactory.zoomTo(map.getCameraPosition().zoom - 0.5f));
Vatic answered 30/6, 2015 at 16:22 Comment(0)
N
14

In onMapReady() Method

change the zoomLevel to any desired value.

float zoomLevel = (float) 18.0;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
Norsworthy answered 28/1, 2018 at 13:44 Comment(0)
N
2

you have to write only one line in maps_activity.xml

map:cameraZoom="13"

I hope this will solve your problem...

Norsworthy answered 28/1, 2018 at 13:38 Comment(0)
S
2

You can use

    CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
Steere answered 7/10, 2018 at 10:57 Comment(0)
G
1

Even i recently had the same query....some how none of the above mentioned setmaxzoom or else map:cameraZoom="13" did not work So i found that the depenedency which i used was old please make sure your dependency for google maps is correct this is the newest use this

compile 'com.google.android.gms:play-services:11.8.0' 
Grubbs answered 7/3, 2018 at 12:51 Comment(0)
D
1

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPlace,15)); This is will not have animation effect.

Dyscrasia answered 16/10, 2019 at 8:52 Comment(0)
A
1

I tried with "mMap.animateCamera( CameraUpdateFactory.zoomTo( 17.0f ) );" but it didn't work for me. So I used this animation for zooming in on start.

LatLng loc = new LatLng(33.8688, 151.2093);
mMap.addMarker(new MarkerOptions().position(loc).title("Sydney"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 18), 5000, null);
Atlantean answered 4/5, 2020 at 20:25 Comment(0)
V
0
Location locaton;
double latitude = location.getlatitude;
double longitude = location.getlongitude;

If you want to save the zoom or get it all the time,you just need to call the following

int zoom = mMap.getCameraPosition().zoom;

//To set that just use

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(getlatitude(), getlongitude),zoom);
Vorster answered 14/9, 2017 at 7:0 Comment(0)
I
0

Most of the answers above are either deprecated or the zoom works by retaining the current latitude and longitude and does not zoom to the exact location you want it to. Add the following code to your onMapReady() method.

@Override
public void onMapReady(GoogleMap googleMap) {
    //Set marker on the map
    googleMap.addMarker(new MarkerOptions().position(new LatLng(0.0000, 0.0000)).title("Marker"));
    //Create a CameraUpdate variable to store the intended location and zoom of the camera
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(0.0000, 0.0000), 13);
    //Animate the zoom using the animateCamera() method
    googleMap.animateCamera(cameraUpdate);
}
Invert answered 17/2, 2020 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.