Using mapbox in scrollview, scrollview sliding to

2019-09-05 02:44发布

问题:

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 mapbox. BTW, I am using Mapbox 4.0.0 Android SDK.

what I want see is:

However the weird thing is as follows:

回答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:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);

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

    // ....

    mapView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(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

ScrollView xmlns: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">


<LinearLayout
    android: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.MapView
        android: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!



回答2:

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        mScrollView.scrollTo(0,0);
    }
});