vicolored
vicolored

Reputation: 835

Android zoom map at specific spot

So far I was testing my map app with google maps on a mobile device.

Cause I wanted to start at a specific place I added this code :

    @Override
    public void onMapReady(GoogleMap gMap) {
    googleMap = gMap;
    gMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    CameraUpdate center =
            CameraUpdateFactory.newLatLng(SKG_VIEW);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(11);

    gMap.moveCamera(center);
    gMap.animateCamera(zoom);

where SKG_VIEW is a LatLng I define in my code.

When I test the same code in a tablet the center is way far from the one I define.

Is it something I can do to fix this? I don't even understand the root of this problem.

Upvotes: 0

Views: 94

Answers (1)

antonio
antonio

Reputation: 18242

Moving the camera to an specific location and changing the zoom sequentially doesn't work on some devices and is not the correct way to do it.

The correct way to do so is to create a CameraPosition using a CameraPosition.Builder, setting the location and zoom level (and bearing, and tilt, ...) on that Buider, and then building it (see the documentation).

In your case, you can do:

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(SKG_VIEW)
    .zoom(11)
    .build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Upvotes: 1

Related Questions