【问题标题】:Map does not detect touches/swipes in android地图未检测到 android 中的触摸/滑动
【发布时间】:2015-12-26 09:13:05
【问题描述】:

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

起初我使用 tabActivity 并且我的地图片段正常工作,看起来像这样:

   mapActivity extends FragmentActivity...

由于不推荐使用 tabActivity,我按照上面的教程添加了滑动选项卡。地图不能用 FragmentActivity 添加,只能用 Fragment 所以我把地图片段改成:

   mapActivity extends Fragment...

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

【问题讨论】:

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


    【解决方案1】:

    试试这个。它的样本。 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();
        }
    }
    

    【讨论】:

    • 感谢您的回复尼克。我试过了,但我得到了这个错误:无法解析符号'GPSTracker'。所以我评论了它和我的应用程序。现在我的地图选项卡没有显示地图,它只是一个蓝屏。
    • 您的代码有效,我只需要编辑 xml 文件,如下所示。
    【解决方案2】:

    一开始我在app_bar_main的appBarLayout中添加了tabLayout和viewPager(我做了一个带有导航抽屉的应用程序),后来我把appBarLayout移到了content_main中,并且它工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多