双击:缩放 Android MapView?

发布于 2024-08-30 01:26:04 字数 73 浏览 4 评论 0原文

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

你能给我一个提示吗?

After a little bit of work my route application works fine.
The only thing I just want to add is a double tap zoom in function, but I don't know how.

Could you give me a hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一刻暧昧 2024-09-06 01:26:04

我也一直在寻找答案/示例,但找不到有效的代码。

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

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

I've also been searching for an answer/example, but found nowhere working code.

Finally, here's the code that's working for me:

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);
  }
}

main.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>

Don't forget to replace here the android:apiKey value with your apiKey.

七婞 2024-09-06 01:26:04

实现 GestureListener 以接收 doubleTap 事件。

请参阅:网格布局上的 Fling 手势检测

Implement GestureListener to receive doubleTap events.

see: Fling gesture detection on grid layout

梦里的微风 2024-09-06 01:26:04

这是一个很好的、简明扼要的实现:

Double Tap Zoom in GoogleMaps Activity

(对于所有对这个问题犹豫不决的人来说,这是一个迟到的答案)

Here is a nice and down-to-the-point implementation:

Double Tap Zoom in GoogleMaps Activity

(a late answer for all those that stuble over this question)

戏蝶舞 2024-09-06 01:26:04

我知道这是一篇旧文章,但这个解决方案也有效: 双击/点击地图缩放 ( Android)

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

An old post, I know, but this solution works too: double click/tap map zoom (Android)

I think he fixed his errors in his own post, so just use the question part, not the answers (they are horrible :))

傲世九天 2024-09-06 01:26:04

当您想要放大和缩小时,请使用以下代码

  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);
  }

Use the following code when you want to zoom in and zoom out

  1. zoom in double click on top 50% of mapview
  2. Zoom out double click on bottom 50% of mapview
@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);
  }
蒲公英的约定 2024-09-06 01:26:04

如果有人正在寻找新 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;

In case somebody is looking for the answer for the new GoogleMap (Since 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文