Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android maps padding and map bounds

I'm using android maps v2, I'm using a padding to move the map controls and the logo of google:

       mMap.setPadding(0, 0, 0, padding_in_px);

On the other hand I need to know what are the limits of the visible part of the map:

       LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;

The problem is that if I put a padding, the function does not return me the bounds of the visible region on the map, but only the bounds of the visible region minus the padding region.

Is there any way that when the map has padding mMap.getProjection().getVisibleRegion() returns the actual visible bounds and not the map bounds - the padding bounds?

Thanks

like image 286
Kunta Kinte Avatar asked Jul 13 '15 17:07

Kunta Kinte


People also ask

What is padding in Android?

Simply, Padding means Push Inside. When we say push inside we mean in a rectangle type view the content pushed itself according to the dimension specified in the padding attribute. Diagrammatically, the concept of Padding is shown as: Syntax: android:padding=”size in dp”

How do I pad the edges of a Google map?

This can be done by calling the setPadding () method on the GoogleMap object. It takes pixels as an integer value to pad the left, top, right, and bottom edges. This means that 400 pixels are padded in the left corner and 100 pixels are padded in the top and right corners. Let's discuss this with an example.

How to choose the bounds of a latlng?

In particular, it will consider extending the bounds both in the eastward and westward directions (one of which may cross the antimeridian) and choose the smaller of the two. In the case that both directions result in a LatLngBounds of the same size, this will extend it in the eastward direction. a LatLng to be included in the new bounds.

How do I make a Google map fit the coordinates?

The natural choice to achieve that would be to use the fitBounds method on the Map object with the bounding rectangle for the coordinates of all results. Unfortunately, though, Google chose to add a non-configurable 45px margin around that bounding box so that in a lot of cases the map appears to be zoomed out too far for what would be possible.


1 Answers

The LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds; can give you the nearLeft and nearRight properties, they are LatLng type.

You can convert either nearLeft or nearRight to a screen point, by using toScreenLocation(). Once you get the bottom point of your visible region, you can use that point plus the padding pixels you put (ex: 100, padding_in_px). Then you can convert back the point back using fromScreenLocation().

LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
LatLng nearLeft = bounds.nearLeft
Point visibleNearLeftPoint =  mMap.getProjection().toScreenLocation(nearLeft);
Point screenBottomLeftPoint = new Point(visibleNearLeftPoint.x, visibleNearLeftPoint.y + padding_in_px);
LatLng screenBottomLatLng = mMap.getProjection().fromScreenLocation(screenBottomLeftPoint);
like image 172
ztan Avatar answered Sep 30 '22 00:09

ztan