【问题标题】:Double tap: zoom on Android MapView?双击:放大 Android MapView?
【发布时间】:2011-02-11 02:01:46
【问题描述】:

经过一些工作,我的路线应用程序运行良好。 我只想添加一个双击放大功能,但我不知道如何。

你能给我一个提示吗?

【问题讨论】:

    标签: java android android-view android-mapview android-touch-event


    【解决方案1】:

    实现 GestureListener 以接收 doubleTap 事件。

    见:Fling gesture detection on grid layout

    【讨论】:

    • 该帖子中没有双击为什么每个人都重定向到该特定线程:|
    【解决方案2】:

    这是一个不错且实用的实现:

    Double Tap Zoom in GoogleMaps Activity

    (对于所有对此问题存疑的人来说,这是一个迟到的答案)

    【讨论】:

      【解决方案3】:

      我知道一个旧帖子,但这个解决方案也有效:double click/tap map zoom (Android)

      我认为他在自己的帖子中修正了他的错误,所以只使用问题部分,而不是答案(它们太可怕了:))

      【讨论】:

        【解决方案4】:

        我也一直在寻找答案/示例,但找不到工作代码。

        最后,这是对我有用的代码:

        MyMapActivity.java

        public class MyMapActivity extends MapActivity
                                   implements OnGestureListener, OnDoubleTapListener {
        
        private MapView mapView;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mapView = (MapView)findViewById(R.id.mapView);
        }
        
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            int x = (int)e.getX(), y = (int)e.getY();;  
            Projection p = mapView.getProjection();  
            mapView.getController().animateTo(p.fromPixels(x, y));
            mapView.getController().zoomIn();  
            return true; 
        }
        
        // Here will be some autogenerated methods too
        

        OnDoubleTap.java

        public class OnDoubleTap extends MapView {
        
          private long lastTouchTime = -1;
        
          public OnDoubleTap(Context context, AttributeSet attrs) {
            super(context, attrs);
          }
        
          @Override
          public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
              long thisTime = System.currentTimeMillis();
              if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
                // Double tap
                this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
                lastTouchTime = -1;
              } else {
                // Too slow 
                lastTouchTime = thisTime;
              }
            }
            return super.onInterceptTouchEvent(ev);
          }
        }
        

        ma​​in.xml

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        
            <azizbekyan.andranik.map.OnDoubleTap
                android:id="@+id/mapView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:enabled="true"
                android:clickable="true"
                android:apiKey="YOUR_API_KEY" />        
        </LinearLayout>
        

        不要忘记将此处的 android:apiKey 值替换为您的 apiKey

        【讨论】:

        • onInterceptTouchEvent 处理程序的来源似乎是this blog post。我通过设置ViewConfiguration.getDoubleTapTimeout()改进了Andrianik的代码。
        【解决方案5】:

        当你想放大和缩小时使用下面的代码

        1. 双击地图视图的前 50% 放大
        2. 双击地图视图底部 50% 的缩小范围

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
              long thisTime = System.currentTimeMillis();
        
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) activity.getSystemService(activity.getApplicationContext().WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        int height = metrics.heightPixels; 
        
        if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
                // Double tap
                 if((height/2)>ev.getY()){
        // Zoom IN
                    this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
                }else{
                    this.getController().zoomOutFixing((int) ev.getX(), (int) ev.getY());
        //Zoom Out
                }
                lastTouchTime = -1;
              } else {
                // Too slow 
                lastTouchTime = thisTime;
              }
            }
            return super.onInterceptTouchEvent(ev);
          }
        

        【讨论】:

          【解决方案6】:

          如果有人正在寻找新 GoogleMap 的答案(自 Google-Play-service:9+)

          case MotionEvent.ACTION_DOWN:
                          x1 = m.getX();
                          if ((System.currentTimeMillis() - systemTime) < 200) {
                              mGoogleMap.animateCamera(CameraUpdateFactory.zoomIn());
                          }
                          systemTime = System.currentTimeMillis();
                          break;
          case MotionEvent.ACTION_UP:
                          systemTime = System.currentTimeMillis();
                          ...
                          break;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-10-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-29
            • 2011-03-07
            • 2011-07-21
            相关资源
            最近更新 更多