【问题标题】:android maps: how to determine map center after a drag has been completedandroid maps:拖动完成后如何确定地图中心
【发布时间】:2010-12-18 22:12:45
【问题描述】:

有没有办法通过android maps API,在平移动画完成后我可以检测到地图中心?我想使用这些信息从服务器动态加载标记。 谢谢 BD

【问题讨论】:

    标签: android maps markers


    【解决方案1】:

    自 2016 年 8 月以来,Android 版地图能够检测诸如 onCameraMoveStarted 之类的事件(以及移动的原因,例如 REASON_GESTUREREASON_DEVELOPER_ANIMATION

    以下代码(主要取自docs)给出了您可以实现的目标:

    public class MyCameraActivity extends FragmentActivity implements
            OnCameraMoveStartedListener,
            OnCameraMoveListener,
            OnCameraMoveCanceledListener,
            OnCameraIdleListener,
            OnMapReadyCallback {
    
        private GoogleMap mMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_camera);
    
            SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap map) {
            mMap = map;
    
            mMap.setOnCameraIdleListener(this);
            mMap.setOnCameraMoveStartedListener(this);
            mMap.setOnCameraMoveListener(this);
            mMap.setOnCameraMoveCanceledListener(this);
    
            // Show Sydney on the map.
            mMap.moveCamera(CameraUpdateFactory
                    .newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
        }
    
        @Override
        public void onCameraMoveStarted(int reason) {
    
            if (reason == OnCameraMoveStartedListener.REASON_GESTURE) {
                Toast.makeText(this, "The user gestured on the map.",
                               Toast.LENGTH_SHORT).show();
            } else if (reason == OnCameraMoveStartedListener
                                    .REASON_API_ANIMATION) {
                Toast.makeText(this, "The user tapped something on the map.",
                               Toast.LENGTH_SHORT).show();
            } else if (reason == OnCameraMoveStartedListener
                                    .REASON_DEVELOPER_ANIMATION) {
                Toast.makeText(this, "The app moved the camera.",
                               Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        public void onCameraMove() {
            Toast.makeText(this, "The camera is moving.",
                           Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onCameraMoveCanceled() {
            Toast.makeText(this, "Camera movement canceled.",
                           Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onCameraIdle() {
            Toast.makeText(this, "The camera has stopped moving.",
                           Toast.LENGTH_SHORT).show();
            // Here you get the camera center
            GeoPoint geoPoint = projection.fromPixels(mMap.getHeight() / 2, mMap.getWidth() / 2);
            double centerLat = (double)geoPoint.getLatitudeE6() / (double)1E6;
            double centerLng = (double)geoPoint.getLongitudeE6() / (double)1E6;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我也一直在寻找一种“确实结束拖动”的解决方案,它可以在地图结束移动后的那一刻检测到地图中心。我还没有找到它,所以我做了这个运行良好的简单实现:

      private class MyMapView extends MapView {
      
          private GeoPoint lastMapCenter;
          private boolean isTouchEnded;
          private boolean isFirstComputeScroll;
      
          public MyMapView(Context context, String apiKey) {
              super(context, apiKey);
              this.lastMapCenter = new GeoPoint(0, 0);
              this.isTouchEnded = false;
              this.isFirstComputeScroll = true;
          }
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              if (event.getAction() == MotionEvent.ACTION_DOWN)
                  this.isTouchEnded = false;
              else if (event.getAction() == MotionEvent.ACTION_UP)
                  this.isTouchEnded = true;
              else if (event.getAction() == MotionEvent.ACTION_MOVE)
                  this.isFirstComputeScroll = true;
              return super.onTouchEvent(event);
          }
          @Override
          public void computeScroll() {
              super.computeScroll();
              if (this.isTouchEnded &&
                  this.lastMapCenter.equals(this.getMapCenter()) &&
                  this.isFirstComputeScroll) {
                  // here you use this.getMapCenter() (e.g. call onEndDrag method)
                  this.isFirstComputeScroll = false;
              }
              else
                  this.lastMapCenter = this.getMapCenter();
          }
      }
      

      就是这样,希望对你有帮助! o/

      【讨论】:

        【解决方案3】:

        您使用的是MapActivity 吗?这是我使用的代码:

        MapView mapView = (MapView)findViewById(R.id.map);
        Projection projection = mapView.getProjection();
        int y = mapView.getHeight() / 2; 
        int x = mapView.getWidth() / 2;
        
        GeoPoint geoPoint = projection.fromPixels(x, y);
        double centerLatitude = (double)geoPoint.getLatitudeE6() / (double)1E6;
        double centerLongitude = (double)geoPoint.getLongitudeE6() / (double)1E6;
        

        您还需要添加类似的代码:

        @Override
        public boolean dispatchTouchEvent(MotionEvent event)
        {
            boolean result = super.dispatchTouchEvent(event);
            if (event.getAction() == MotionEvent.ACTION_UP)
                reload_map_data();    ///  call the first block of code here
            return result;
        }
        

        【讨论】:

        • 这是否可以处理如果您(在您抬起手指后)扔地图会继续移动一点点的“扔”?
        • 嗨。感谢marcc的回复,但我需要找到一种方法来检测平移动画何时停止,以便那时我可以使用您的中心计算代码。
        猜你喜欢
        • 2015-12-29
        • 1970-01-01
        • 2014-05-29
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多