Skip to content Skip to sidebar Skip to footer

Using Mapbox In Scrollview, Scrollview Sliding To The Position Where Mapbox Is Put In The Page

Now, when I use mapbox in scrollview, scrollview would slide to the position where mapbox is located in the page, but I want to see the top of the page at the first sight, not the

Solution 1:

If I understand your question correctly, you are having issues panning/zooming the mapview while it is in a scrollview? This questions been asked here a lot lately...

This snippet of code might resolve the issue for you:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    finalScrollViewsv= (ScrollView) findViewById(R.id.scrollView);

    // Setup the MapView
    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    // ....

    mapView.setOnTouchListener(newView.OnTouchListener() {
        @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    sv.requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    sv.requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return mapView.onTouchEvent(event);
        }
    });

Lastly, heres my XML

ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:mapbox="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.cameron.mapboxplayground.MainActivity"android:id="@+id/scrollView">


<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- Set the starting camera position and map style using xml--><com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="500dp"mapbox:access_token="@string/accessToken"mapbox:style_url="@string/style_light"mapbox:center_latitude="40.7359"mapbox:center_longitude="-74.0151"mapbox:zoom="10"/></LinearLayout>
</ScrollView>

Hope this helps you out!

Solution 2:

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(newOnMapReadyCallback() {
    @OverridepublicvoidonMapReady(MapboxMap mapboxMap) {
        mScrollView.scrollTo(0,0);
    }
});

Post a Comment for "Using Mapbox In Scrollview, Scrollview Sliding To The Position Where Mapbox Is Put In The Page"