【问题标题】:Map freezes, does not allow zooming after being added in sliders tab地图冻结,添加到滑块选项卡后不允许缩放
【发布时间】:2015-12-27 06:54:53
【问题描述】:

我正在尝试将地图添加到选项卡。我首先使用 tabActivity 执行此操作,但由于它已被弃用,我决定使用滑动选项卡和导航抽屉再次进行此操作。我关注this tutorial 并设法让它工作,但现在我遇到了一个问题 - 当我尝试用手指移动地图时,它变得非常慢,它会拖动,并且只移动一点点。我遇到的另一个问题是放大/缩小 - 我只能在双击时放大而不能缩小。另外,地图上下移动也被禁用了。

我的地图片段:

    public class MapsActivity extends Fragment {

    private GoogleMap mMap;
    MapView mapView;
    Marker marker; // Marker
    int markerCount = 0; // Marker counter

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflate and return the layout
    View v = inflater.inflate(R.layout.activity_maps, container,
            false);
    mapView = (MapView) v.findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    mapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mMap = mapView.getMap();
    mMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    double latitude = myLocation.getLatitude();
    double longitude = myLocation.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_me)));

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        int iMax = 5; // Max number of markers

        @Override
        public void onMapLongClick(LatLng arg0) {

            if (markerCount < iMax) {
                // start SendMessageActivity need to add marker to message activity
                Intent intent = new Intent(getActivity(), SendInvitationActivity.class);
                startActivity(intent);

                markerCount = markerCount + 1;
                marker = mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_0))
                        .position(
                                new LatLng(arg0.latitude,
                                        arg0.longitude))
                        .visible(true));
            } else {
                Toast.makeText(getActivity().getBaseContext(), "Only " + iMax + " markers allowed at the same time",
                        Toast.LENGTH_LONG).show();
            }
        }
     });
     return v;
    }

    @Override
    public void onResume() {
      super.onResume();
      mapView.onResume();
    }

    @Override
      public void onPause() {
         super.onPause();
         mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

和xml:

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

      <com.google.android.gms.maps.MapView
         android:id="@+id/mapView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         class="com.google.android.gms.maps.SupportMapFragment" />
   </LinearLayout>

所以我的问题是我的地图对手指输入的反应很差。当我使用 TabActivity 时,情况并非如此。我想知道这可能是滑块问题 - 因为我滑动标签在它们之间移动,这会“混淆”地图吗?

我试过this,但它在地图上不起作用,它只是禁用了标签之间的滑动。

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    我想通了。我最初在 app_bar_main.xml 的 Toolbar 下方添加了 TabLayout 和 ViewPager:

       <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white" />
       </android.support.design.widget.AppBarLayout>
    

    在我从 app_bar_main.xml 中删除整个 AppBarLayout 并将其添加到 content_main.xml 之后,它就可以工作了没有任何故障。

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2020-06-09
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多