5

I want to move camera to position to fit LatLngBounds with regard to marker height. So far I'm able for fit the anchors of markers with this code:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markerList) {
    builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

int padding = getActivity().getResources().getDimensionPixelSize(R.dimen.home_map_padding); // offset from edges of the map in pixels
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mGoogleMap.animateCamera(cameraUpdate);

It produces output like this: my output so far As you can see there's empty space at the bottom (this is int padding). I'd like to have the padding to be equal like this:

enter image description here

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103

2 Answers2

5

Your question is similar to the one posted here:

Defining a CameraUpdate with LatLngBounds with different padding values

The solution is to set the top padding of the actual GoogleMap to the height of your marker icon.

Something like this will do:

Drawable drawable = <YOUR DRAWABLE HERE>;
final int padding = drawable.getIntrinsicHeight();
map.setPadding(0, padding, 0, 0);

Looks like this with my custom marker image (rulers inserted for comparison):

enter image description here

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94
  • In fact my question is a duplicate. But anyway you saved my day (or even few of them). – Marian Paździoch Mar 10 '15 at 12:35
  • @MarianPaździoch Glad that my answer was helpful. I was actually searching for a solution myself and found your question (and the other one). – Baz Mar 10 '15 at 12:38
  • Wrong because indicator automatically move –  Nov 05 '15 at 20:02
  • @delive Can you elaborate? – Baz Nov 05 '15 at 20:13
  • Yes see my post : http://stackoverflow.com/questions/33552160/map-v2-how-to-add-margin-marker-is-possile/33552754#33552754 –  Nov 05 '15 at 20:30
  • 1
    @delive There is no post from you there and the answer there pretty much says the same I suggested, so what is it you are saying? – Baz Nov 05 '15 at 21:15
0

I don't believe there's any way to get the height of the marker unless you're using a custom marker. However given that the markers stay the same size you can scroll the camera view up by a set amount.

You're going to have to play around a bit with how much to scroll by but once you get the value you can then scale it based on screen density if necessary.

Kony2013
  • 171
  • 9
  • The problem is: I know marker height (in pixels) but I don't know what will be map zoom after animation so I can't reflect that height in padding here: https://developer.android.com/reference/com/google/android/gms/maps/CameraUpdateFactory.html#newLatLngBounds(com.google.android.gms.maps.model.LatLngBounds,%20int) – Marian Paździoch Mar 09 '15 at 10:50