Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep location centered, when changing Google Maps padding in Android

Let's assume at the center of the map is the Eiffel Tower, when I change the bottom padding to half of the screen height, the Eiffel Tower keeps at it's position. What I want is to move the Eiffel Tower to middle of upper half of the screen. Can someone help me?

like image 204
JPLauber Avatar asked Jun 01 '16 12:06

JPLauber


Video Answer


1 Answers

Based from this documentation, you can add padding around the edges of the map using the GoogleMap.setPadding() method. The map will continue to fill the entire container, but text and control positioning, map gestures, and camera movements will behave as if it has been placed in a smaller space.

Here is my sample code:

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

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

        LatLng eiffel = new LatLng(48.858093, 2.294694);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(eiffel));
        mMap.addMarker(new MarkerOptions().position(eiffel).title("Marker in Eiffel"));
    }

Here is the screenshot:

Without padding:

Without padding

With padding:

With padding

As you can see, I have set a padding at the bottom and the Google logo is on the middle of the screen which proves that the padding is working. The marker is also at the middle of upper screen.

You can also check on this video tutorial the way on how to use map padding with the Google Maps Android API.

Hope it helps! :)

like image 160
abielita Avatar answered Oct 31 '22 06:10

abielita