Center Android GoogleMap on one point while zooming just enough to include another point

android, google-maps-android-api-2

Solution

Your own answer is overcomplicated.

You need to calculate point that is on the other side of your position and include that in `LatLngBounds`.

LatLng otherSidePos = new LatLng(2 * yourPos.latitude - destPos.latitude, 2 * yourPos.longitude - destPos.longitude);
bounds = new LatLngBounds.Builder().include(destPos)
                .include(otherSidePos).build();
        map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));

Note: you don't want to hardcode 50 pixels as padding. It will look different on different devices. Use density independent pixels instead.

Problem

I would like to have something like a radar screen that centers on where the user is while zooming just enough to include a target point with the v2 api. Right now I'm using ``` bounds = new LatLngBounds.Builder().include(destPos) .include(yourPos).build(); map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50)); ``` but this centers at some point between the two points and zooms to include both. Is there a way as easy as this to do what I want? Or do I have to more or less start from scratch and do some math (e.g. calculate the distance between the two points, calculate the lat/lng's for LatLngBounds so that user is at the center of the defined rectangle and the edges of the rectangle include the destination -- taking into consideration the map/screen dimensions)? Here's what I have: Here's what I want:

Original source

Related problems