Android v2 MapFragment shaking when scrolling in Scrollview
Asked Answered
W

2

2

I am using the SupportMapFragment to display a static map in a ScrollView. I do not like to move / zoom the Map, just showing where the location is.

When I am scrolling down/up the map shakes inside its bounds, it feels pretty laggy. My question is, how is ist possible to remove this lag, or how to use a static version of the v2 api map.

This is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin_half"
    android:background="@color/white"
    android:orientation="vertical" >

  ...

    <fragment
        android:id="@+id/fragmentMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/viewDivider"
        android:layout_margin="@dimen/margin_half"

         />

    <View
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignBottom="@id/fragmentMap"
        android:layout_alignLeft="@id/fragmentMap"
        android:layout_alignRight="@id/fragmentMap"
        android:layout_alignTop="@id/fragmentMap"
        android:background="@android:color/transparent" />

   ...

</RelativeLayout>

This is not really related to MapFragment in ScrollView, even though i used this trick to remove the black clipping on older devices, as you can see with the transparent view.

I also disabled all gestures and the click-ability of the maps view:

getMap().getUiSettings().setAllGesturesEnabled(false);
getMap().getUiSettings().setZoomControlsEnabled(false);
getMap().getUiSettings().setMyLocationButtonEnabled(false);

...
mapView.setClickable(false);
mapView.setFocusable(false);
mapView.setDuplicateParentStateEnabled(false);
Wavemeter answered 24/7, 2013 at 9:11 Comment(0)
W
1

With the new release of the Google Play Services, Google added a snapshot method in order to retrieve a Bitmap of the currenrt shown map, which instead may be shown. So interaction is not possible but at leased the map is not shaking anymore.

use

GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)

and implement the SnapshotReadyCallback. Once the Snapshot is delivered, replace the MapFragment with an ImageView containing the Snapshot.

This example works pretty well in the onResume method:

@Override
    public void onResume() {
        super.onResume();
        notifyDataChanged();
        final ViewTreeObserver viewTreeObserver = fragmentMap.getView()
                .getViewTreeObserver();

        if (viewTreeObserver.isAlive()) {
            viewTreeObserver
                    .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                        @Override
                        public void onGlobalLayout() {
                            onMesuredMap(this);
                        }
                    });
        }

    }

    private void onMesuredMap(OnGlobalLayoutListener listener) {
        if (fragmentMap.getView().getWidth() != 0
                && fragmentMap.getView().getHeight() != 0) {

            fragmentMap.getView().getViewTreeObserver()
                    .removeOnGlobalLayoutListener(listener);

            makeSnapShot();
        }
    }

        private void makeSnapShot() {
    Runnable action = new Runnable() {
        @Override
        public void run() {

            fragmentMap.getMap().snapshot(new SnapshotReadyCallback() {

                @Override
                public void onSnapshotReady(Bitmap arg0) {
                    imageViewStaticMap.setImageBitmap(arg0);
                    removeMapFragment();
                }

            });
        }
    };
            //litle hack to make sure that the map is loaded for sure
    fragmentMap.getView().postDelayed(action, 1500);
}

    private void removeMapFragment() {
        if (fragmentMap.isVisible()) {
            getChildFragmentManager()
                    .beginTransaction()
                    .remove(fragmentMap)
                    .commit();
        }
    }

Sidenote: To use the new release run an Update of the Android SDK Manager, and when using maven run the maven-android-sdk-deployer with:

mvn install -fae
Wavemeter answered 13/8, 2013 at 14:55 Comment(0)
I
0

Try using this class, it should look better at least when scrolling right and left in our scrollview:

public class TransparentSupportMapFragment extends SupportMapFragment {
    public TransparentSupportMapFragment() {

        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle savedInstance) {

        View layout = super.onCreateView(inflater, view, savedInstance);
        FrameLayout frameLayout = new FrameLayout(getActivity());
        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return layout;
    }
}
Impractical answered 24/7, 2013 at 9:12 Comment(3)
Unfortunatly it does not solve the vertical scrolling lag, but it is a more beautiful solution of the transparent overlay. Thanks.Wavemeter
About your issu i don't see a solution for now, i've always used google map in full screen in an activityImpractical
This is bad news. Maybe I will use API v1 again.Wavemeter

© 2022 - 2024 — McMap. All rights reserved.