6

I need to add extra padding around the markers on the map that I show.

Here is the code so far but the top and bottom or west and east markers are on the edge of the screen.

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (int i = 0; i < arrayList.size(); i++) {
                options.position(latlngs.get(i));
                options.title(arrayList.get(i).getArea().toString());
                options.snippet(arrayList.get(i).getRegion().toString());
                mMap.addMarker(options);
                builder.include(latlngs.get(i));
            }
            LatLngBounds bounds = builder.build();
            int padding = 0;
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);

            mMap.moveCamera(cameraUpdate);            }
    });

}

Here is what I get: What I get currently

This is what I want: What I need So how can I add some padding so that I can see all the markers but not have the markers butted against the edge of the map? Thanks for any input.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
timv
  • 3,346
  • 4
  • 34
  • 43

4 Answers4

7

Set padding value > 0
Example:
int padding = 50;

Shweta Nandha
  • 728
  • 1
  • 8
  • 19
  • 1
    50 seems to work well, I think I tried 5 and it was not working and assumed the padding was affecting other aspect of the map. Good to have that solved. Many thanks, – timv Dec 02 '16 at 11:41
6

I would also suggest solution by @Zumry Mohamed from the thread: Android map v2 zoom to show all the markers, that worked best for me:

width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
padding = (int) (width * 0.20); // offset from edges of the map - 20% of screen
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,, width, height, padding);
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
3

You already had the padding value in your code.

Just increase the value of int padding = 0; to a higher value (for example int padding = 100;)

antonio
  • 18,044
  • 4
  • 45
  • 61
0

This is for setting padding to the map. In the below method you can make map to focus the camera on latlngbounds and padding you give will be given from all for side.

CameraUpdateFactory.newLatLngBounds(LatLngBounds bounds, int padding)

The second method which you can use for specifying padding in a particular direction:

GoogleMap.setPadding(     10,      20,      20,     50);

Here arguments are in pixel(Int), left, top, right, bottom. I used both the above methods in following way.

fun moveCameraToPolyLine(polyline: Polyline, googleMap: GoogleMap?) {
    val builder = moveCameraToPolyline(polyline)
    builder.let {builder->
        googleMap?.let {googleMap ->
            googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder!!.build(), 300))
setDirectionalPaddingToGoogleMap(300,300,300,600)
        }
    }
}

//left,top,right,bottom in pixels
fun setDirectionalPaddingToGoogleMap(googleMap: GoogleMap?,left :Int,top : Int, right : Int, bottom : Int){
    googleMap?.setPadding(left,top,right,bottom)
}
Sahil Bansal
  • 609
  • 8
  • 6