Google Maps v2 Set Zoom Level
Asked Answered
A

5

18

When I launch Google maps in Android, it shows the whole world. What do i need to change so as to set the zoom to the marker? Here are the codes. Thanks

        googlemap.getUiSettings().setZoomControlsEnabled(true);
    googlemap.getUiSettings().setMyLocationButtonEnabled(true);


    LatLng cameraLatLng = sfLatLng;
    float cameraZoom = 17;


    if(savedInstanceState != null){
        mapType = savedInstanceState.getInt("map_type", GoogleMap.MAP_TYPE_NORMAL);

        double savedLat = savedInstanceState.getDouble("lat");
        double savedLng = savedInstanceState.getDouble("lng");
        cameraLatLng = new LatLng(savedLat, savedLng);

        cameraZoom = savedInstanceState.getFloat("zoom", 30);

        googlemap.setMapType(mapType);
        googlemap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraLatLng, cameraZoom));
Alecalecia answered 8/6, 2013 at 9:41 Comment(0)
P
48

try this---->

  map.addMarker(new MarkerOptions().position(latlng)).setVisible(true);

  // Move the camera instantly to location with a zoom of 15.
  map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));

  // Zoom in, animating the camera.
  map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
Podium answered 8/6, 2013 at 11:16 Comment(6)
Worked very well. Thanks alotAlecalecia
how to do the same (i.e setting zoom level) when using LatLngBounds?Nadeau
@AllTimeKing-Narendra I don't know but a guy answered here. I hope this will help.Podium
Do you know perhaps how to set the zoom so that the entire world would show? Something like this: https://mcmap.net/q/102634/-how-to-show-entire-world-and-put-markers-on-it/878126 ?Bathysphere
@androiddeveloper Google Maps API, try to set zoom level to 1 which will show the world view.Podium
@MrRoshanPawar I already tried. It doesn't show entire world. It barely even show continents.Bathysphere
G
6
mMap.animateCamera(CameraUpdateFactory.zoomTo(19));
Genitive answered 24/8, 2016 at 3:42 Comment(0)
C
5

The following code may do the trick:

    function addMarker(lat, lng, info) {
    var pt = new google.maps.LatLng(lat, lng);
    bounds.extend(pt);

You can try the following too:

    var pt = new google.maps.LatLng(lat, lng);
    map.setCenter(pt);
    map.setZoom(your desired zoom);

Edit:

 map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );//where 14.0 is the zoom level
Collectivism answered 8/6, 2013 at 9:43 Comment(1)
Is set Position the Same as setCenter, if so ,i have already done that, but there is no property for setZoom...Alecalecia
I
1

If you have a Place (or bounds), you could zoom to it. E.g. zoom to specific country, city or address with .newLatLngBounds() method:

map.animateCamera(CameraUpdateFactory.newLatLngBounds(place.getViewport(), 10)); // 10 is padding

Zoom level will be selected automatically. It works perfectly with location search.

Isabelisabelita answered 1/12, 2015 at 6:49 Comment(0)
W
0
    private GoogleMap mMap;


   @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMinZoomPreference(3.0f);
        mMap.setMaxZoomPreference(5.5f);
    }
Wast answered 10/7, 2017 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.