Chad Precilla
Chad Precilla

Reputation: 57

How to zoom in on google maps location?

When I open the app, it shows the entire world and I have to keep zooming in to view the place properly.How do I make the camera zoom in about 11 times as if I were to keep double clicking?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions()
                .position(new LatLng( 10.625176, -61.354915))
                .title("Tru-Valu, Trincity Mall"));
    }

Upvotes: 0

Views: 193

Answers (3)

Aleksandar Ilic
Aleksandar Ilic

Reputation: 1546

GoogleMap has "move" and "animate" methods for positioning and zooming.

You can get CameraUpdate object using CameraUpdateFactory. Here are some examples.

  • CameraUpdateFactory.newLatLng(LatLng latLng)

Returns a CameraUpdate that moves the center of the screen to a latitude and longitude specified by a LatLng object. This centers the camera on the LatLng object.

  • CameraUpdateFactory.newLatLngZoom(LatLng latLng, float zoom)

Returns a CameraUpdate that moves the center of the screen to a latitude and longitude specified by a LatLng object, and moves to the given zoom level.

  • CameraUpdateFactory.zoomBy(float amount)

Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint.

You can find other helper methods in CameraUpdateFactory documenation. Also, here is very usefull documentation from Google Developers regarding moving the GoogleMap's camera.

So, if you would like to zoom at Tru-Valu, Trincity Mall place, this is how your code should look like

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(10.625176, -61.354915), 16f));

Upvotes: 1

Lucas Crawford
Lucas Crawford

Reputation: 3118

Similar to Milad Nouri's answer, initially the user might not have Location on, and the FusedLocationProvider won't have the last location yet, because the API client hasn't started. So, my trick for this is to save the user' last Latitude and Longitude to savedPreferences and load this location as the LatLng for the initial camera animation when the app starts.

I save the LatLng when it is updated last so it just remembers the last Latitude and Longitude that was updated for the user's location on the map.

Upvotes: 0

Milad Nouri
Milad Nouri

Reputation: 1597

try this:

LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);

Upvotes: 0

Related Questions