Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GoogleMap v2 getting cropped or not fully displayed

I am observing unexpected behavior of Google Map. Problem is Map getting crop from top and bottom area using Support Map Fragment, if I am using full screen, map is completely visible as shown in Picture 1, but if I am applying map fragment height or weight property, map is not completely visible as shown in Picture 2.

layout xml for picture 1:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.paki.venturedive.awtest.MapsActivity" />

layout xml for picture 2:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.6"
    tools:context="com.paki.venturedive.awtest.MapsActivity" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello world"
    android:layout_weight="0.4" />

Java code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

In Picture 2, I am(User will be) unable to see arctic ocean above Greenland area, similarly bottom area region will be missing, as shown in Picture 3enter image description here.

Did anyone face this issue or does anyone know how to cope up it with?

Any reference link or hint will be kindly appreciated.

Picture 1, Picture 2.

like image 867
Abdul Wahab Avatar asked Oct 30 '22 06:10

Abdul Wahab


1 Answers

Use this snippet. I got what you were saying, by taking the map fragment inside Linear/Relative layout, the map gets cut from top and bottom and some portions are not visible. Since you want map to take 60% of the screen one possible solution is, take the map fragment inside FrameLayout with height as match_parent.

Now, take the textview inside LinearLayout with parent as FrameLayout and set its gravity to be bottom and set its height dynamically 40% of the screen(or how much your requirement is) by using the DisplayMetrics class. By this way, we are ensuring that the full screen is provided to the map so that no portion gets cut, and displaying the text also above the map. I'm attaching the updated screenshot also.

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_framecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"

        android:layout_height="match_parent" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:id="@+id/tv_main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fragment_map"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:text="Hello world" />

    </LinearLayout>
</FrameLayout>

The java code in your onCreate for DisplayMetrics:

   DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            mylinearlayout.setMinimumHeight((int)(metrics.heightPixels * 0.4));

Screenshot updated image with full map on top

I think this will help.

like image 122
Mrigank Avatar answered Nov 09 '22 05:11

Mrigank