【问题标题】:Android MapView only scrolling verticallyAndroid MapView 只能垂直滚动
【发布时间】:2016-06-16 12:31:29
【问题描述】:

我的问题:我只能在 MapView 中上下滚动,而不是左右滚动。

我的 LinearLayout 中有一个 MapView,它由嵌套滚动视图保存。所以我的 layout.xml 看起来像这样:

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

    ...

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_margin="10dp"
        android:id="@+id/jd_map_view"/>
    ...
</LinearLayout>

我正在我的 Fragment 中初始化 MapView,由 TabLayout 保存,如下所示:

mapView = (MapView) mRootView.findViewById(R.id.jd_map_view);
mapView.onCreate(savedInst);
mapView.onResume();
mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                LatLng latlng = new LatLng(mLat, mLng);
                googleMap.addMarker(new MarkerOptions().position(latlng));
                CameraUpdate center= CameraUpdateFactory.newLatLng(latlng);
                CameraUpdate zoom=CameraUpdateFactory.zoomTo(14);

                googleMap.moveCamera(center);
                googleMap.animateCamera(zoom);
            }
        });

它或多或少地正常工作,这意味着我看到了一张地图,看到了我的标记,并且地图位于正确的位置。但很遗憾,我不能向右滚动...

【问题讨论】:

  • 你的意思是在哪里使用框架布局?而不是线性布局或作为 MapFragment 的占位符?
  • 它不起作用:/ 是否有可能,我必须强制 NestedScrollView 让触摸通过 MapView?
  • 很好,谢谢 :)

标签: android google-maps scrollview android-mapview nestedscrollview


【解决方案1】:

这不适合你吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
       android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

【讨论】:

  • 很遗憾没有:/
【解决方案2】:

如果你把它放在一个简单的框架布局中,地图会自动向任何方向滚动,而不是使用滚动布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/main"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="thiscode.databasedemo.Main">

    <TextView
        android:id="@+id/tv"
        // adjust margins for your need.
        android:layout_marginTop="30dp"
        // etc


    <FrameLayout
        android:id="@+id/fl"
        android:layout_below="@+id/tv"
        android:layout_height="fill_parent"
        // adjust margins for your need.
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="40dp"
        android:layout_width="match_parent">

        <fragment
            android:id="@+id/map"
            android:layout_height="fill_parent"
            android:layout_width="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"/>
    </FrameLayout>
</RelativeLayout>

活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    // create class object
    fragmentManager = getFragmentManager();
    //
    try {
        GoogleMap googleMap =
                ((MapFragment) fragmentManager.findFragmentById(R.id.map))
                        .getMap();
        // example data.
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        LatLng latlng = new LatLng(0.083037, 106.704897);
        CameraPosition cameraPosition =
                new CameraPosition.Builder().target(latlng).zoom(2).build();
        googleMap.animateCamera(
                CameraUpdateFactory.newCameraPosition(cameraPosition));
    } catch (Exception e) {
        e.printStackTrace();
    }

【讨论】:

  • 谢谢,我试试。只是为了避免误解:我需要 NestedScrollView 因为内容太大,而不是地图。我想要一个普通的 ScrollView,其中有一个 MapView,如果我在其中滚动,它就会激活。
  • 进一步:我想在片段中显示地图,而不是在活动中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
相关资源
最近更新 更多