Set Map Bounds with different padding
Asked Answered
Z

3

15

I want to know if ther is any way of set different padding from each site of the Device for Google maps bounds.

Because I have an mpview witch match the whole activity, but I have a MapOverlay (some Text inside a linerar Layout with non transulent background) on the bottom 1/3 off the screen.

And now i want to zoom in by using map bounds. But it should have more padding from the bottom so it didn't get behind my mapOverlay.

I use this code with the padding of 120.

final com.google.android.gms.maps.model.LatLngBounds.Builder bounds = new LatLngBounds.Builder();
bounds.include(pref.getSavedLatLng());
bounds.include(currentLatLng);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 120));

Is there any way of setting different paddings, or other solution for this problem?

Thank you for helping and sorry for my english.

Zosima answered 27/11, 2013 at 19:40 Comment(1)
C
48

This is an older question and maybe the asker found the solution but here it is for anyone who faces same problem in future. Set the individual padding using

googleMap.setPadding(left, top, right, bottom)

and then use

googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 0))

Don't forget to set the paddings back to 0 (or whatever they were before) after you are done. Because unlike newLatLngBounds(), the paddings set by setPadding() are permanent.

Constrictive answered 14/1, 2015 at 6:50 Comment(4)
This doesn't work for me, because it will also add a padding to the Google logo in the bottom left corner so that it floats around on the map.Lim
This works but the Google icon will be come to middle of the screen!Tabathatabb
@LakpriyaSenevirathna just use below 3 lines. googleMap.setPadding(left, top, right, bottom) googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 0)) googleMap.setPadding(0, 0, 0, 0) You will not even notice the google icon moving.Wil
Hackish, but works in flutter as well.Indelible
M
0

The question is old but for more informations please check here.

If the bound is supposed to be applied for a specific action, setting the map padding may not be a good solution cause it's permanent.

apply a padding of 100 and above to see the effect in your map. Bellow is a simple example.

final int MAP_BOUND_PADDING = 180;  /* In dp */  
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : listLatLng) { //listLatLngis a list of latLng to bound. You can put two if you want. 
    builder.include(latLng);
}
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, MAP_BOUND_PADDING);
mMap.animateCamera(cu);
Mobile answered 24/1, 2018 at 9:58 Comment(0)
T
0

This can be used but here we have small animation issue!

mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50), new GoogleMap.CancelableCallback() {
                @Override
                public void onFinish() {
                    mMap.animateCamera(CameraUpdateFactory.zoomBy(-1));
                }

                @Override
                public void onCancel() {

                }
            });
Tabathatabb answered 8/7, 2021 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.