地图冻结,在滑块选项卡

时间:2015-12-25 14:20:38

标签: java android google-maps android-fragments

我正在尝试将地图添加到标签中。我首先使用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时并非如此。我想知道这可能是滑块问题 - 因为我滑动标签在它们之间移动,这可能会导致混淆&#34;地图?

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

1 个答案:

答案 0 :(得分:0)

我明白了。我最初在app_bar_main.xml中的工具栏下面添加了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 后,它可以正常工作没有任何故障。