benaich
benaich

Reputation: 922

How to change only the tilt in Google Map API

My application use Google map API v2, I'd like to change the tilt if the user reaches a zoom level greater than 16

Here is what I've done so far

@Override
public void onMapReady(GoogleMap map) {
    //..    
    mMap = map;
    mMap.setOnCameraChangeListener(getCameraChangeListener());
}

public OnCameraChangeListener getCameraChangeListener()
{
    return new OnCameraChangeListener() 
    {
        @Override
        public void onCameraChange(CameraPosition position) 
        {
            Log.d(MainActivity.TAG, "Zoom: " + position.zoom);
            if(position.zoom > 16){
                // change tilt here
            }
        }
    };
}

As you can see I've added a listener to the map object, that trigger whenever position of the camera change, but I don't know how to change only the tilt of the camera

Upvotes: 2

Views: 5221

Answers (2)

benaich
benaich

Reputation: 922

i've added a OnCameraChangeListener to the map object

mMap.setOnCameraChangeListener(getCameraChangeListener());

change the tilt to 60 if the zoom is greater than 15

public OnCameraChangeListener getCameraChangeListener()
{
    return new OnCameraChangeListener() 
    {
        @Override
        public void onCameraChange(CameraPosition position) 
        {
            int mCameraTilt = (position.zoom < 15) ? 0 : 60;
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(
                    new Builder()
                    .target(position.target)
                    .tilt(mCameraTilt)
                    .zoom(position.zoom)
                    .build()));
        }
    };
}

Upvotes: 10

Andy
Andy

Reputation: 2414

Do you mean tilting the map? You can do so by setting the tilt field of a CameraPosition.

Create a new CameraUpdate using CameraUpdateFactory.newCameraPosition(CameraPosition) where CameraPosition is newly built (in this case with the same parameters as position, aside from the tilt). You can then use moveCamera to apply the change.

Upvotes: 1

Related Questions