Map没有检测到android中的触摸/滑动

时间:2015-12-26 09:13:05

标签: android google-maps android-fragments android-studio

我跟着this tutorial将地图添加到我的某个标签中。它可以工作,但是当我打开地图选项卡时,我无法向上/向下滑动,放大/缩小(除非我双击)。向左/向右滑动有效,但地图移动很少。我制作了一个customViewPager,它禁用了滑动选项卡,但没有为他映射做任何事情。

起初我使用了tabActivity,我的地图片段按原样运行,看起来像这样:

   mapActivity extends FragmentActivity...

由于tabActivity已弃用,我按照上面的教程添加了滑动标签。无法使用FragmentActivity添加地图,只能使用Fragment,因此我将地图片段更改为:

   mapActivity extends Fragment...

现在我遇到以下问题 - 我无法浏览地图。未检测到滑动和触摸输入。我无法上下移动,放大或缩小,我唯一能做的就是从左向右移动,但速度非常慢。

2 个答案:

答案 0 :(得分:2)

试试这个。它的样本。 100%工作

public class Map extends Fragment {

    final int RQS_GooglePlayServices = 1;
    Location myLocation;
    TextView tvLocInfo;
    boolean markerClicked;
    PolygonOptions polygonOptions;
    Polygon polygon;
    // GPSTracker class
    GPSTracker gps;
    double latitude;
    double longitude;
    MapView mapView;
    GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_map_fragment, container, false);
        // TODO Auto-generated method stub
        MapsInitializer.initialize(getActivity());

        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                mapView = (MapView) rootView.findViewById(R.id.map);
                mapView.onCreate(savedInstanceState);
                // Gets to GoogleMap from the MapView and does initialization stuff
                if (mapView != null) {
                    map = mapView.getMap();
                    map.getUiSettings().setMyLocationButtonEnabled(false);
                    map.setMyLocationEnabled(true);
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 13);
                    map.animateCamera(cameraUpdate);
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
                Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
        }

        gps = new GPSTracker(getActivity());

        if (gps.canGetLocation()) {

            latitude = gps.getLatitude();
            longitude = gps.getLongitude();

            // \n is for new line
            //      Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
        } else {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }


        //    tvLocInfo = (TextView) findViewById(R.id.locinfo);

      /*  FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment
                = (MapFragment) myFragmentManager.findFragmentById(R.id.map);
        map = myMapFragment.getMap();*/

        map.setMyLocationEnabled(true);

        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);


        markerClicked = false;

        map.setMyLocationEnabled(true);
        map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {

                CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
                CameraUpdate zoom = CameraUpdateFactory.zoomTo(11);
                map.moveCamera(center);
                map.animateCamera(zoom);

            }
        });


      /*  LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();


        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
        if (location != null)
        {
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 13));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }*/
        // Updates the location and zoom of the MapView


        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
     /*   TextView tv = (TextView) getActivity().findViewById(R.id.textset);
        tv.setText("Audio");*/
    }

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

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

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

答案 1 :(得分:2)

首先我在app_bar_main中添加了tabLayout和viewPager到appBarLayout(我用导航抽屉创建了一个应用程序),后来我将appBarLayout移动到了content_main并且它运行了。